Jump to content

My first script.. need help


pro-mos

Recommended Posts

So i started learning scripting a week ago, and now i made my first script, the script lets the player to set a nick of his choice without being limited to MTA nick length, it show only in chat..

the problem is that the script is server side, and changes nick for all people.. and i don't know how to make it client side script because "onPlayerChat" event is only server side.

I tried using triggerServerEvent, but got so confused and banged my head to the wall... :?

Help, here is my code:

meta.xml

  
<meta> 
    <info author="[TPI]Mos" type="script" /> 
    <script src="mos.lua" type="server" /> 
</meta> 
  

mos.lua

  
function customNick(thePlayer, command, nick) 
    if nick == nil then 
        outputChatBox("Mos: You need to type a new name, you fucktard!", source, 200,70,0, true) 
    else 
        chatNick = nick 
        outputChatBox("* " ..getPlayerName(thePlayer) .." #FFFFFFis now " ..chatNick, root, 255,255,255, true) 
    end 
end 
addCommandHandler("dsnr", customNick, true) 
  
function customNickChats(message) 
    if chatNick ~= nil then 
        cancelEvent() 
        outputChatBox(chatNick .."#FFFFFF: " ..message, root, 255,255,255, true) 
        outputServerLog("CHAT: "..chatNick..": "..message) 
    end 
end 
addEventHandler("onPlayerChat", root, customNickChats) 
  

please tell me whats the solution and how can improve my code?

and is it possible to change the nick in TAB also the nametag on top of the player?

Edited by Guest
Link to comment

Put it in an array:

local chatNick = {} 
  
addEventHandler ( "onPlayerJoin", root, function ( ) 
    chatNick[source] = getPlayerName(source) 
end ) 
  
addEventHandler ( "onPlayerQuit", root, function ( ) 
    chatNick[source] = nil       
end ) 
  
addCommandHandler ( "whatever", function ( player, cmd, newname ) 
    chatNick[player] = newname 
end ) 
  
  
outputChatBox ( chatNick[source] ... ) 

Link to comment

There are several ways, but the way which Bonus gave you is seriously a nice way to go for. something which I like to do is set the data on the player his head with setElementData. No matter where you need the nickname, you can always retrieve it by using getElementData instead of calling your resource for the actual nickname. An approach would be;

addCommandHandler ( "dsnr", 
    function ( thePlayer, _, newNick ) 
        if ( newNick ) then 
            setElementData ( thePlayer, "player:customNick", newNick ); 
            outputChatBox ( "Mos: Your new nickname has been assigned, new messages will now use the nickname: ".. newNick, thePlayer, 70, 200, 0, true ); 
            return; 
        end 
        outputChatBox ( "Mos: You need to type a new name, you !", source, 200, 70, 0, true ); 
    end, true 
); 
  
addEventHandler ( "onPlayerchat", getRootElement(), 
    function ( message ) 
        cancelEvent(); 
        local customNick = getElementData ( source, "player:customNick" ); 
        outputChatBox ( ( customNick and customNick or getPlayerName ( source ) ) .."#FFFFFF: ".. message, root, 255, 255, 255, true ); 
        outputServerLog ( "CHAT: ".. ( customNick and customNick or getPlayerName ( source ) ) ..": ".. message ); 
    end 
); 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...