pro-mos Posted February 11, 2016 Share Posted February 11, 2016 (edited) 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 February 11, 2016 by Guest Link to comment
Dimos7 Posted February 11, 2016 Share Posted February 11, 2016 addEvnetHandler("onClientPlayerChnageNick", localPlayer, function(oldNick, newNick) outputChatBox(oldNick..."is known as"..newNick) end ) Link to comment
pro-mos Posted February 11, 2016 Author Share Posted February 11, 2016 my code has nothing to do with that event.. Link to comment
Bonus Posted February 11, 2016 Share Posted February 11, 2016 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
tosfera Posted February 11, 2016 Share Posted February 11, 2016 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
pro-mos Posted February 11, 2016 Author Share Posted February 11, 2016 Bonus and Tosfera's codes both amazed me, first time to see these methods, even i don't 100% understand it but still looks very nice Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now