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

Why does my script not run when the PlayersAlive Value changes?

The main reason for this script is to determine the winner, but that's the only thing that it can't do. I've tried numerous times to find where the error is but the output doesn't come out with one. I don't know if anyone is even going to be willing to be reading all of the script which is why I held back on going to this option for so long but I hope it gets solved. Thanks in advance.

--// Variables

local Status = game.ReplicatedStorage.StatusValue
local PlayersAlive = game.ReplicatedStorage.PlayersAlive

--// Functions
function FindIndex(Table, Val)
	for index, v in pairs(Table, Val) do
		if v == Val then
			return index
		end
	end
end

--//Main Script
local debounce = false
script.Parent.Touched:connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h and debounce ~= true then
		debounce = true
		local a = hit.Parent.Name
		workspace[a]:FindFirstChild("Torso").CFrame = CFrame.new(workspace.Lobby.Spawn.Position)
		if game.ReplicatedStorage.Players:FindFirstChild(a) then game.ReplicatedStorage.Players:FindFirstChild(a):Destroy() end
		PlayersAlive.Value = PlayersAlive.Value - #game.ReplicatedStorage.Players:GetChildren()
			wait()
		debounce = false
		
		workspace.MapStorage:ClearAllChildren()
		_G.RoundInProgress = false
	elseif #game.ReplicatedStorage.Players:GetChildren() == 0 then
		Status.Value = "Everybody has lost the game :("
		workspace.MapStorage:ClearAllChildren()
		_G.RoundInProgress = false
		else return end
end)

game.ReplicatedStorage.PlayersAlive.Changed:connect(function()
	if PlayersAlive.Value == 1 then
			for i, v in pairs(game.ReplicatedStorage.PlayersAlive:GetChildren()) do
				local a = v.Parent.Name
				print(a)
				if game.ReplicatedStorage:FindFirstChild(a) then
					game.Players[a].leaderstats.Coins.Value = game.Players[a].leaderstats.Coins.Value + 200
					game.Playres[a].leaderstats.Wins.Value = game.Players[a].leaderstats.Wins.Value + 1
					Status.Value = a.. "has won the game!"
					wait(1)
				end
			end
		end
end)

1 answer

✓ Accepted answer

Let us go through common issues in this scenario. This is a list of possible issues. You could easily have put the script in a bad place. For example, since there is no output, your script not might be running. Make sure your script is not disabled. Secondly, place your script in ServerScriptService, and see if it runs then. If it is, your script will not run in its old location.

If not, then the cause must be the .Changed() event not triggering. That could be for a numerous reasons, the most obvious being the value not actually changing.

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