So basically I made a script where a forcefield gets destroyed when a stat becomes a certain number, but the thing is, it doesn’t work. No error shows up and I’ve tried lots of things but nothing seems to work. Here is the script:
game.Players.PlayerAdded:Connect(function(player)
local stat = player:WaitForChild("Zombies Killed") --the stat
if stat.Value >= 21 then
print("all zombies killed")
script.Parent:Destroy() -- the parent is the forcefield
end
end)
The problem is that you are currently only checking the value of “Zombies Killed” when the player first joins. The solution is to use the Changed event and check the value of the stat every time it changes.
game.Players.PlayerAdded:Connect(function(player)
local stat = player:WaitForChild("Zombies Killed") -- the stat
stat.Changed:Connect(function(zombiesKilled)
if zombiesKilled >= 21 then
print("all zombies killed")
script.Parent:Destroy() -- the parent is the forcefield
end
end)
end)
I did test the code above and verified it functions as expected. Most likely you are changing the value on the client instead of the server which is the problem.
game.Players.PlayerAdded:Connect(function(player)
local stat = player:WaitForChild("Zombies Killed")
stat:GetPropertyChangedSignal('Value'):Connect(function()
if stat.Value >= 21 then
print("all zombies killed")
script.Parent:Destroy()
end
end)
end)