steadyfi Posted March 6, 2015 Share Posted March 6, 2015 Hi. I have a little problem with passing the localPlayer variable from Client-Side to Server-Side with a triggerServerEvent() Client-Side Code: local localPlayer = getLocalPlayer() function teamSetText() team = getPlayerTeam(getLocalPlayer()) if team then guiSetText(teamNameLabel, "Team: "..getTeamName(getPlayerTeam(getLocalPlayer()))) guiSetText(playerCountLabel, "Players: "..countPlayersInTeam(getPlayerTeam(getLocalPlayer()))) triggerServerEvent("openteams.getTeamLeader", localPlayer) end end Server-Side Code: function giveClientLeaderName(localPlayer) local team = getPlayerTeam(localPlayer) if team then --XML local xmlFile = xmlLoadFile("teams.xml", "root") local children = xmlNodeGetChildren(xmlFile) for i,node in pairs(children) do local attribute = xmlNodeGetAttribute(node, "name") if attribute == getTeamName(team) then local leaderName = xmlNodeGetAttribute(team, "leader") end end xmlUnloadFile(xmlFile) --END XML triggerClientEvent("openteams.getTeamLeader.serverGave", leaderName) end end addEvent("openteams.getTeamLeader", true) addEventHandler("openteams.getTeamLeader", getRootElement(), giveClientLeaderName) Error: It says it expected the player element but it got nil (it wasn't passed) Link to comment
JR10 Posted March 6, 2015 Share Posted March 6, 2015 The second parameter of triggerServerEvent is a baseElement. If you pass the localPlayer in triggerServerEvent as the second argument, it will be sent as the predefined variable source, so in giveClientLeaderName, 'source' is the localPlayer. This is discouraged though as it is vulnerable to event-faking. Read the wiki page for more info. Link to comment
steadyfi Posted March 6, 2015 Author Share Posted March 6, 2015 The second parameter of triggerServerEvent is a baseElement. If you pass the localPlayer in triggerServerEvent as the second argument, it will be sent as the predefined variable source, so in giveClientLeaderName, 'source' is the localPlayer.This is discouraged though as it is vulnerable to event-faking. Read the wiki page for more info. Thank you. By the way, could you give me the link to the wiki page ? Link to comment
JR10 Posted March 6, 2015 Share Posted March 6, 2015 https://wiki.multitheftauto.com/wiki/TriggerServerEvent In any Lua code on the forums you can click the functions which will direct you to its respective wiki pages. Link to comment
steadyfi Posted March 6, 2015 Author Share Posted March 6, 2015 Also, 1. Security isn't important, all I do is passing localPlayer Server-Side, getting a string, and passing it back Client-Side 2. It now says an event is not triggerable. Client Side: local localPlayer = getLocalPlayer() function teamSetText() team = getPlayerTeam(getLocalPlayer()) if team then guiSetText(teamNameLabel, "Team: "..getTeamName(getPlayerTeam(getLocalPlayer()))) guiSetText(playerCountLabel, "Players: "..countPlayersInTeam(getPlayerTeam(getLocalPlayer()))) triggerServerEvent("openteams.getTeamLeader", localPlayer) end end function serverGaveLeaderName(leaderName) team = getPlayerTeam(getLocalPlayer()) if team then guiSetText(teamNameLabel, "Leader: "..tostring(leaderName)) end end addEvent("openteams.getTeamLeader.serverGave") addEventHandler("openteams.getTeamLeader.serverGave", getRootElement(), serverGaveLeaderName) Server-Side: function giveClientLeaderName() local team = getPlayerTeam(source) if team then --XML local xmlFile = xmlLoadFile("teams.xml", "root") local children = xmlNodeGetChildren(xmlFile) for i,node in pairs(children) do local attribute = xmlNodeGetAttribute(node, "name") if attribute == getTeamName(team) then local leaderName = xmlNodeGetAttribute(node, "leader") end end xmlUnloadFile(xmlFile) --END XML triggerClientEvent("openteams.getTeamLeader.serverGave", root, leaderName) end end addEvent("openteams.getTeamLeader", true) addEventHandler("openteams.getTeamLeader", getRootElement(), giveClientLeaderName) Error: Server triggered clientside event openteams.getLeaderName.serverGave, but event is not marked as remotly triggerable Link to comment
Bonsai Posted March 6, 2015 Share Posted March 6, 2015 addEvent("openteams.getTeamLeader.serverGave") needs to be addEvent("openteams.getTeamLeader.serverGave", true) Link to comment
steadyfi Posted March 6, 2015 Author Share Posted March 6, 2015 addEvent("openteams.getTeamLeader.serverGave") needs to be addEvent("openteams.getTeamLeader.serverGave", true) Thank you. #SLOVED 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