Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Player attribute not updating when function is called?

I have a script which assigns a local attribute 'playing' to every player which first enters the game.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(modelPlayer)
		local playing = Instance.new("BoolValue")
		playing.Name = "playing"
		playing.Parent = player
	end)
end)

This works fine and the player will have a playing bool in the explorer through Players. I also have another script which changes the bool to true from false when they press a GUI button which successfully changes it in the explorer. (This is not the issue, the bool is definitely updated in the explorer I have checked)

The problem arises when I have a server script which checks if the player has their 'playing' bool value set to true in order for them to be teleported out of the lobby however even if their bool has been updated successfully when the function is called it does not teleport them and the console still outputs that their value is 'false'

function MovePlayers(...)
    local spawns = {...}
    for _, plr in pairs(game:GetService("Players"):GetPlayers()) do -- iterate through table of players
		local playing = plr.playing.Value --Check if the player's playing bool means they can be teleported
		print(playing)
        if plr.Character and playing == true then -- if character exists and has playing set to true then
            plr.Character.Torso.CFrame = CFrame.new(spawns[math.random(#spawns)]) -- move their torso to a random spawn
        end
    end
end

If however I set the players playing bool value to 'true' at the very start, this function does detect that their value is true and will teleport them. I am quite confused as to why it will work if the value is initially assigned to true but not if it was initially false and then changed to true later on. I would appreciate any help, thanks!

1 answer

✓ Accepted answer

If a boolvalue has been set to true via localscript, then a serverscript would see it as still false. To fix this, you have to set the bool value from a server script or have the check run from a localscript or use remote events to tell the server to change the bool.

If you want to use remoteevent then this example should get you started:

--Server script (serverscriptservice)
local event = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))
event.Name = "SetPlaying"

event.OnServerEvent:Connect(function(player, boolean)
    if player and type(true) == type(boolean) then
        local boolvalue = player:WaitForChild("playing")
        if nil ~= boolvalue then
            boolvalue.Value = boolean
        end
    end
end)

From your localscript:

--local script
--where ever you set your boolvalue playing to true, use this instead
game:GetService("ReplicatedStorage"):WaitForChild("SetPlaying"):FireServer(true) -- use this for setting to false aswell

2 comments

0

Thanks worked, appreciate the explanation and the detailed code response. It's always these little things I'm not aware about in lua that get me, is there anywhere I can learn stuff like that? I had no idea a local script wouldn't update it but I guess it makes sense.

0

You're absolutely welcome! Thank you or feedback.

This discussion is preserved as it was. New answers, comments, and votes are closed.