Regen script for car not working

So it works but it doesn’t have a cooldown. I have put wait() in different spots to try and get a cooldown nothing is working.
Script:

location = script.Parent.Parent.Parent
regen = script.Parent.Parent
save = regen:clone()

script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("HumanoidRootPart") then
	
	wait(10)
	back = save:clone()
	back.Parent = location
	back:MakeJoints()

end
end)

you gotta make a flag variable in the if statement

local onCooldown = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("HumanoidRootPart") and not onCooldown then
        back = save:clone()
        back.Parent = location
        back:MakeJoints()

        onCooldown = true
        wait(10)
        onCooldown = false
    end
end)
local location = script.Parent.Parent.Parent
local regen = script.Parent.Parent
local save = regen:clone()
local debounce = false

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("HumanoidRootPart") and not debounce then
	debounce = true
	back = save:clone()
	back.Parent = location
	back:MakeJoints()
    wait(10)
    debounce = false
end
end)

Your debounce or cooldown should begin immediately so that the touched function doesn’t keep firing.

1 Like

It still created lots of cars and lagged out roblox studio

It still created lots of cars and lagged out roblox

Show your explorer hierarchy… because currently you have the car model stored in the part you touch? It should be located in ReplicatedStorage.

Also you are cloning the car in the variable up top called Save… you don’t need to clone it there because you are cloning it in the touched function.

Also where are you trying to parent the car to? workspace? This is why we need to see your hierarchy.