Jump to content

[Help] localPlayer passing failed


steadyfi

Recommended Posts

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

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
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

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

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...