(help)Global Variable is nil in client side

Hey, i got a global variable called currentTeam
If the player joins (serverside), it does

currentTeam = 'groove' --default team
TriggerEvent('gw:teamGet', source, function(team)
        if team == 'groove' then
            currentTeam = 'groove'
        elseif team == 'ballas' then
            currentTeam = 'ballas'
        else
            currentTeam = currentTeam
        end
    end)

that works fine!
but If I call the variable in client side

RegisterCommand('currentTeam', function()
    local source = GetPlayerServerId(PlayerId())
    TriggerServerEvent('gw:teamCheck', source)
    print(currentTeam)
end)

it gives me nil back

if I write print(currentTeam) in server side its working…
only in client side its giving me nil value

The variable is serverside, that means clientside doesn’t know about it

1 Like

aren’t global variables global to both?

Nope, they stay on the client or server. You have to send the variable in an event

1 Like

Where is this event ? ‘gw:teamCheck’

1 Like

here:

RegisterNetEvent('gw:teamGet')
AddEventHandler('gw:teamGet', function(source, callback)
    TriggerEvent('es:getPlayerFromId', source, function(user)
        getPlayerTeam(user.getIdentifier(), function(team)
            callback(team)
        end)
    end)
end)

I got another problem / question.

function getPlayerTeam(identifier, callback)
    if identifier ~= nil and type(identifier) == "string" then
        MySQL.Async.fetchAll("SELECT team FROM team WHERE identifier = @identifier", {["@identifier"] = identifier}, 
            function (result)
            if result[1] then callback(result[1].team) 
            else callback(nil) end
        end)
    else
        print("Error occurred while checking existance user, missing parameter or incorrect parameter: identifier")
    end
end

RegisterCommand('getTeam', function(source)
    getPlayerTeam(GetPlayerIdentifier(source, 0), function(team)
        print("Your TEAM: "..team)
    end)
end)

that is server sided, it works perfectly.
But if I try this in client side, team == nil value …

RegisterCommand('currentTeam', function()
    getPlayerTeam(GetPlayerServerId(PlayerId()), function(team)
        print("Your TEAM: "..team)
    end)
end)

You call teamcheck instead of teamget.

1 Like

my bad thats the event

RegisterNetEvent('gw:teamCheck')
AddEventHandler('gw:teamCheck', function(source)
    TriggerEvent('gw:teamGet', source, function(team)
        if team == 'groove' then
            currentTeam = 'groove'
        elseif team == 'ballas' then
            currentTeam = 'ballas'
        else
            currentTeam = team
        end
    end)
end)
1 Like

You call the event but you don’t send the value from server to the client this way.

1 Like

How could I do that?
maybe with return currentTeam or how

Not tested

server 

TriggerClientEvent('getTeam:currentTeam',-1,currentTeam)

client 

currentTeam = {}

RegisterNetEvent('getTeam:currentTeam')
AddEventHandler('getTeam:currentTeam', function(currentTeam)

currentTeam = currentTeam

end
end)
RegisterCommand('currentTeam', function()
   
    print(currentTeam)
end)

so almost working…
If I call the event, the print is sent with the right team

currentTeam = ''
-- Event to get variable currentTeam in client side
RegisterNetEvent('gw:currentTeamSend')
AddEventHandler('gw:currentTeamSend', function(currentTeam)
    currentTeam = currentTeam
    print("CurrentTeam:"..currentTeam)
end)

But if I try this its showing me nil

RegisterCommand('currentTeam', function()
    print(CurrentTeam)
end)

why is it showing nil if the global value is now in client side

I can’t test

server 
RegisterNetEvent('callTeam:currentTeam')
AddEventHandler('calTeam:currentTeam', function()
TriggerClientEvent('getTeam:currentTeam',-1,currentTeam)
end)


client 

currentTeam = {}

RegisterNetEvent('getTeam:currentTeam')
AddEventHandler('getTeam:currentTeam', function(currentTeam)

  currentTeam = currentTeam

end)



RegisterCommand('currentTeam', function()
   TriggerServerEvent('callTeam:currentTeam')
   Wait(100)--maybe useless just to let the time for value returning
   print(currentTeam)
end)

or try

" currentTeam = {}

instead of:

" currentTeam = ‘’ "

1 Like

If I run this in client

RegisterCommand('currentTeam', function()
    TriggerServerEvent('gw:currentTeamGet')
    Wait(1000)
    print('test'..currentTeam)
end)

the event currentTeamGet is beeing called but
currentTeam is showing nothing
in output I see only: test

or try

currentTeam = {}

instead of:

currentTeam = “”

1 Like

thats very weird… now the print is completly not showing but the events is beeing called

with that?

1 Like

yes :C

currentTeam = {}
-- Event to get variable currentTeam in client side
RegisterNetEvent('gw:currentTeamSend')
AddEventHandler('gw:currentTeamSend', function(currentTeam)
    currentTeam = currentTeam
    print("TEAM="..currentTeam)
end)

RegisterCommand('currentTeam', function()
    TriggerServerEvent('gw:currentTeamGet')
    Wait(1000)
    print('test'..currentTeam)
    print("TEST")
end)

the print TEAM=…currentName is working but the command not