Okay, so I am making a CTF (Capture the flag) game and I made a touched flag script.
but when I ran it, my computer froze then rebooted! It could very possibly be that it ran for too much time, but how do I fix it?
I have not tried any solutions, because I am afraid it will happen again, can someone please help me fix it? I am afraid that it will do something to my computer…
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if(player.Team == game.Teams.Red) then
clone = script.Parent.Parent:Clone()
clone.Parent = hit.Parent
weld = Instance.new("Weld")
weld.Parent = clone
weld.Part0 = clone
weld.Part1 = hit.Parent.UpperTorso
elseif(player.Team == game.Teams.Red) then
if(weld == nil and clone ~= nil) then
There is no debounce in your code. You might want to implement one.
Here is some pseudocode:
local last_use = tick()
local COOLDOWN = 3 -- in seconds
when your part is touched
if tick() - last_use < COOLDOWN then -- still need to wait
-- Continue code since you finished waiting
last_use = tick()
The reason this crashed is because you are cloning the script’s parent’s parent, which ends up cloning the script, meaning the object that you welded to the character instantly touches the character, and runs the weld code again. I would weld a fake version of whatever it is you’re welding to the character, rather than copying the trigger model. The debounce you added is great but this example will still infinitely make copies of itself inside of the character over time with debounce.