Need help with my regen button

I’m making a regen button for my roller coaster cart as every now and then it tends to lag and roll back. I made this script, and it runs very good and very smooth as intended. But when I click the button for the second,third,forth, etc. time it just doesn’t work. It will not repeat and I have no idea why.

local cartM = script.Parent.Parent.Parent
local delaytime = .2
script.Parent.cd.MouseClick:Connect(function()
	script.Parent.Parent.Parent.cart:Destroy()
	wait(delaytime)
	local cart =game.ServerStorage.Regenvehicles["MEG cart"]:Clone()
	cart.Parent = cartM
end)

what if you try putting “local cartM = script.Parent.Parent.Parent” in the function

Set the cart’s primary part cframe to the previous cart’s primary part cframe:

local cartM = script.Parent.Parent.Parent
local delaytime = .2
script.Parent.cd.MouseClick:Connect(function()
local CF = script.Parent.Parent.cart.PrimaryPart.CFrame
	script.Parent.Parent.Parent.cart:Destroy()
	wait(delaytime)
	local cart =game.ServerStorage.Regenvehicles["MEG cart"]:Clone()
cart:SetPrimaryPartCFrame(CF)
	cart.Parent = cartM
end)

The problem may be since the function is called at the beginning with .MouseClick:Connect(function() It may only recognize this once. Try replacing the function with a while true do loop

You may also want to make the delay time a bit longer

But I want it to run once then the button needs to be pressed before it runs again

this didnt work it stopped it all together

this didnt do anything same problem as b4

I know, try something like this

local cartM = script.Parent.Parent.Parent
local delaytime = .2
while true do
    if script.Parent.cd.MouseButton1Click then
        script.Parent.Parent.Parent.cart:Destroy()
        wait(delaytime)
        local cart = game.ServerStorage.Regenvehicles["MEG cart"]:Clone()
        cart.Parent = cartM
    end
end

The parentheses on the end at the bottom is wrong it has a red underline

Try doing it without the parentheses

it makes the “end” red and the cartM red

My bad, I got really goofed up with the ends. See if it works now

nothing happens the button did nothing

There’s no need putting it in a while loop since the MouseClick event fires whenever the detector is clicked

This should be it, and if not, I’m sorry.

local cartM = script.Parent.Parent.Parent
local delaytime = .2
    if script.Parent.cd.MouseClick then
        script.Parent.Parent.Parent.cart:Destroy()
        wait(delaytime)
        local cart = game.ServerStorage.Regenvehicles["MEG cart"]:Clone()
        cart.Parent = cartM
end

It didnt work do u know anybody who could help me with this??

When you click the thing once, it works right? But before clicking it again could you send us a screenshot of ServerStorage?

It has been fixed. I just had to change up a few things