Forcefield not destroying when stat equals a certain number?

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)
2 Likes

So I tried using that same script and I changed the value via explorer but nothing happened

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.

You can use GetPropertyChangedSignal(‘Value’)

i think this would works

 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)

I’m not that experienced in scripting, i just change the value in the intvalue inside the player, how do you change the value on the server?

You can switch between the server and client data models through the “HOME” tab.gATcb41kcD

1 Like