Time and select random player not function

Hello. I am trying to make a Voting System. When the voting time ends it was supposed to pick up a random player to be the monster. But when the voting time reaches to zero in the gui, it continues to -1, -2, etc.

local players
local selected

local debounce = true

function check(text)
repeat
wait(0.1)
until debounce == true
debounce = false
script.Parent.Text = text
wait(3)
debounce = true
end

game.ReplicatedStorage.Time.Changed:Connect(function()
if debounce == true then
script.Parent.Text = game.ReplicatedStorage.Manager.Value…" ("…game.ReplicatedStorage.Time.Value…")"
if game.ReplicatedStorage.Time.Value == 0 then do
wait(1)
script.Parent.Text = "The Mystery/MonsterName is " … selected
end
end
end
end)

game.ReplicatedStorage.NotEnoughPlayers.OnClientEvent:Connect(function()
if debounce == true then
debounce = false
script.Parent.Text = “Waiting for more players…”
wait(3)
debounce = true
else
local text = “Waiting for more players…”
check(text)
end

end)

game.ReplicatedStorage.SendTextToPlayers.OnClientEvent:Connect(function(text)
if debounce == true then
debounce=false
script.Parent.Text = text
wait(3)
debounce=true
else
check(text)
end
end)

–Get Random Player:

local function selectPlayer()
players = game.Players:GetPlayers()
selected = players[math.random(1,#players)]
return selected
end

if game.ServerScriptService.VotingSystem.Script.Script2.LobbyTime == 0 then do
wait (0.5)
selectPlayer()
end
end

Is there anyone who can explain to me why is this happening?

Sort of hard to interpret your code as it is not formatted.
However, it seems that your if statement-

-is only run once in your code. So if the Value is not == 0, the code inside your statement will not run, and as the if statement is not looped, it will not check again.

You should consider putting a loop on your check.

Alternatively, you could use RBXScriptSignal .Changed or :GetPropertyChangedSignal() to listen for when the value reaches 0. ← This would be more efficient than adding a loop

1 Like

Oh right, common problems. Thanks :wink:

1 Like