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

My script for checking the amount of true values in a global variable isn't working?

As the title says, I have a script that tries to have the amount of true variables in a count but it isn't working. I also have another script that is laid out the exact same way for another variable and that one works.

There also isn't an error.

wait(10)
_G.playersInGame = 0
local players = game.Players
for I, v in pairs(players:GetChildren()) do
	wait(.01)
	if v:WaitForChild("Backpack").isntOut.Value == true then
		_G.playersInGame = _G.playersInGame + 1
	end
	_G.playersInGame = 0
end

1 answer

I don't recommend using the global table for storing variables, as it is setup and maintained by Roblox; Roblox scripts each have a global environment. If you need to pass values between scripts, I would recommend using *Value objects or BindableEvents/Functions, etc.

That said; you are adding 1 to playersInGame each time you count true, and then setting playersInGame to 0 before the next iteration.

if v:WaitForChild("Backpack").isntOut.Value == true then
	_G.playersInGame = _G.playersInGame + 1
end
_G.playersInGame = 0

Roblox also has a minimum wait() time, so I would recommend forgoing passing a parameter to wait();

-- these are essentially the same
wait(0.01);
wait();

3 comments

0

I just tried doing it with a value object and it also didn't work.

0

DId you remove the line "playersInGame = 0" following the if statement? That's the only reason the code wouldn't count correctly.

0

Yeah, still nothing happens.

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