How do I destroy a script after it's used?

  1. What do you want to achieve?
    I have a brick that changes the walkspeed of the player after walking over it, and then changing the speed back to the normal walkspeed of my game. I’m trying to find a way to create code that makes it so if you step over the brick a second time, the script won’t function.

  2. What is the issue? I can’t find a reasonable way to achieve this.
    image

  3. What solutions have you tried so far?
    I’ve tried several small lines, but they don’t seem to do anything.

2 Likes

Have you tried using script.Disabled = true? It will disable the script and stop it from running again until you set script.Disabled = false. For example:

--Script continues above
wait(10)
hit.Parent.Humanoid.Walkspeed = 11
script.Disabled = true --This will make the script never run again until you set script.Disabled = false.
--Script continues below

Edit: As an alternative, you could also put a debounce in.

local debounce = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if debounce == false then
            debounce = true
            hit.Parent.Humanoid.WalkSpeed = 4
            wait(10)
            hit.Parent.Humanoid.WalkSpeed = 11
        end
    end
end)
3 Likes

Hi so I am not super sure but I did a little look on youtube and this is what I found.

Try this
I did not test this so tell me does not work.
Also learn more about :Distroy() and more built in functions here

Script.Parent.Touched:connect(function(hit)
if hit.Parent:findFirstChild(“Humanoid”) then
hit.Parent.Humanoid.Walkspeed = 4
Script:Distroy()

just use script.Disabled = true
or script:Destroy()
script:Remove() work to but is not the best choice

See more information on “Destroy” here
https://developer.roblox.com/en-us/api-reference/function/Instance/Destroy

Is not Distory, isDestroy()

1 Like

First you would need to identify the first and second time, I think the way you want this script to work is set it 4 then goes to 11 if you touch again it does the same (Tell me if it aint like that)

local debounce = true
local ammountOfTimesTouched = 0
script.Parent.Touched:Connect(function(hit)
       if debounce then
         if hit.Parent:FindFirstChild("Humanoid") then
            debounce = false
            if ammountOfTimesTouched and ammountOfTimesTouched == 2 then
               script:Destroy()
            end 
            ammountOfTimesTouched = ammountOfTimesTouched + 1
            hit.Parent.Humanoid.WalkSpeed = 4
            wait(10)
            hit.Parent.Humanoid.WalkSpeed = 11
            debounce = true
            end
       end
end)

What you’d want to do is call :Disconnect on the connection whenever it’s no longer needed. This will save resources and is good practice to do in the future!

Also, please send code in text format. It makes it much easier to modify.
Modified code:

local event = (script.Parent.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.WalkSpeed = 4
			wait(10)
			hit.Parent.WalkSpeed = 11
			event:Disconnect()
		end
	end)
)

Let me know if anything breaks. hth.

local hitonce = false
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hitonce ~= true then
		hitonce = true
		hit.Parent.Humanoid.WalkSpeed = 4
		wait(10)
		hit.Parent.Humanoid.WalkSpeed = 11
	end
end)

Try this, that should be what you’re looking for @SilentSeedsYT.

and hitonce ~= true
-- checks if the part has already been hit, and if not it runs:
hitonce = true
hit.Parent.Humanoid.WalkSpeed = 4
wait(10)
hit.Parent.Humanoid.WalkSpeed = 11

This worked great! Thanks, I’ll definitely use this command more!

1 Like

You should just throw an error, since script:Destroy() won’t do anything.