I'm trying to script a block that destroys itself after the IntValue inside of it reaches the number 2. The following code is in a Script which is placed in the IntValue. I included the wait statement in case the problem was just that something didn't load, but that didn't fix it. There are no error messages. Nothing happens in output, so the function just isn't running. The block doesn't destroy itself even when the IntValue reaches 2.
block = script.Parent.Parent dmg = script.Parent repeat wait() until dmg ~= nil dmg:GetPropertyChangedSignal("Value"):Connect(function() print("changed") if dmg.Value >= 2 then block:Destroy() print("destroy") else if dmg.Value < 2 then print("safe") end end end)
Any help is super appreciated.
Use local variables.
Using a repeat
loop is absolutely pointless. The function fires whenever the value is changed. (Also, the script is just stuck at the loop.)
Fixed script:
local block = script.Parent.Parent local dmg = script.Parent dmg:GetPropertyChangedSignal("Value"):Connect(function() print("changed") if dmg.Value >= 2 then block:Destroy() print("destroy") elseif dmg.Value < 2 then print("safe") end end)
I resolved this by moving everything into one script. Rather than using an IntValue, I made a local counter value that destroyed the block after it was reached within the tool's script. This has some bugs to be worked out such as random blocks I don't want to be destroyed being included, but I'll figure that out shortly. Thanks to everyone who helped!
--- VARS --- local tool = script.Parent.Parent -- refers to tool container local player = game.Players.LocalPlayer -- finds local player who's running game local mouse = player:GetMouse() -- finds the players cursor distance = 8 -- distance you need to be from block local num = 1 -- baseline amt of damage dealt debounce = false local dmg = 2 -- total dmg before destroy local counter = 0 --- DAMAGE --- tool.Activated:Connect(function() if mouse.Target ~= nil and debounce == false and mouse.Target.ClassName == "Part" and (mouse.Target.Position - script.Parent.Position).Magnitude < distance then debounce = true wait(.75) debounce = false counter = counter + 1 print(counter) end end) -- DESTROY -- tool.Activated:Connect(function() if counter >= dmg - 1 then mouse.Target:Remove() counter = 0 end end)