Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Changed event not working for some reason?

Asked by 8 years ago

Whenever a player joins the game, the code below successfully adds a bool value and sets it to true, and the rest of the code is supposed to print whenever the value of the bool is changed. However, it's not printing anything.

local Player = game:GetService("Players")

function onPlayerAdded(player)
    local bool = Instance.new("BoolValue", player)
    bool.Name = "AliveStatus"
    bool.Value = 1
    print('Welcome, ' .. player.Name)
end

game.Players.PlayerAdded:connect(onPlayerAdded)


while true do
    wait(1)

for i,v in pairs(game.Players:GetChildren()) do
    status = v.AliveStatus
    print(v.Name)
    status.Changed:connect(function()
        if(status.Value == 1) then
            print('yes')
        end
        if(status.Value == 0) then
            print('no') 
        end
    end)

    status.Value = 0
end
end

I'm not really sure what the problem is, any help is appreciated.

p.s. there are no errors, and the script does seem to run fine, as it prints the users name every second like it's supposed to, which makes me think it's running through the whole function, the changed event just isn't working :s

Cheers. -Almorpheus

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

BoolValue stands for boolean value. You have 0 and 1, which are binary representations of booleans, but in Lua, booleans are represented by true and false. So, the value of a BoolValue must be either true or false.

local Player = game:GetService("Players")

function onPlayerAdded(player)
    local bool = Instance.new("BoolValue", player)
    bool.Name = "AliveStatus"
    bool.Value = true
    print('Welcome, ' .. player.Name)
end

game.Players.PlayerAdded:connect(onPlayerAdded)

while true do
    wait(1)
    for i,v in pairs(game.Players:GetChildren()) do
        status = v.AliveStatus
        print(v.Name)
        status.Changed:connect(function()
            if(status.Value == true) then
                print('yes')
            else -- Since it can only be false otherwise, you can condense it into if & else
                print('no') 
            end
        end)

        status.Value = false
    end
end

Hope this helped.

0
Thanks! Worked perfectly. Almorpheus 10 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad

Answer this question