Touched script crashed my whole computer! What do I do to fix?

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 clone
local weld
script.Parent.Touched:Connect(function(hit)
    if(game.Players:GetPlayerFromCharacter(hit.Parent)) then
        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
                clone:Destroy()
            end
        end
    end
end)
1 Like

did it actually reboot it

Anyways I believe this is related: RightGrip Mass Replication Exploit Crashing Servers

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
        return
    end

    -- Continue code since you finished waiting
    last_use = tick()

Yes it actually did reboot my computer, thanks for answering, let me try that :grinning:

Debounce worked! Thank you very much!

1 Like

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.

Hope you can make it work!

2 Likes