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

Changed event not working for some reason?

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

✓ Accepted answer

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.

2 comments

0

Thanks! Worked perfectly.

0

No problem.

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