Why the script work only 1 time

  1. What do you want to achieve? Making a script that clone RoadModel to CloneTo Position and delete the old Road (RoadModel)

  2. What is the issue? The script work only 1 time

  3. What solutions have you tried so far? Check the script and see if there is any issue and see the output if there is any errors but I didn’t saw anything wrong

This is the script

local player = game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats2")
local Dif = leaderstats:WaitForChild("Dif")	
local part = script.Parent
part.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Clone = game.Workspace.RoadModel:Clone()
local NewPosition = CFrame.new(game.Workspace.RoadModel.CloneTo.Position)
Clone:SetPrimaryPartCFrame(NewPosition)
Clone.Parent = workspace
Dif.Value = Dif.Value -1	
script.Disabled = true	
wait(1)	
part.Parent:Destroy()
workspace.Obstacles:Destroy()
local Obstacles	= Instance.new("Folder", workspace)	
Obstacles.Name = "Obstacles"
script.Disabled = false	
print("Done")		
end)		
end)

Uh, I don’t know if I’m just seeing it incorrectly, but are you disabling the script mid-way through? I think that is the issue. Additionally, you are destroying the parent itself which deletes all children as well, including the script.

2 Likes

I disable the script in the mid to avoid spam + the script destroy the road and do the other things (one time only) and if i destroy the road and destroy the script its clone so its come back

Okay, but all this stuff will never be executed:

And a disabled script won’t be listening for playeradded events, I guess thats why it only works one time.

local Players = game:GetService("Players")
local Part = script.Parent
local Debounce = true

Players.PlayerAdded:Connect(function()
    Part.Touched:Connect(function(hit)
        local Player = Players:GetPlayerFromCharacter(hit.Parent)

        if Player and Debounce then
            Debounce = not Debounce

            local Leaderstats = Player:WaitForChild("leaderstats2")
            local Dif = Leaderstats:WaitForChild("Dif")	
            local Clone = workspace.RoadModel:Clone()
            local NewPosition = CFrame.new(workspace.RoadModel.CloneTo.Position)

            Clone:SetPrimaryPartCFrame(NewPosition)
            Clone.Parent = workspace
            Dif.Value -= 1

            task.wait(1)
            Part.Parent:Destroy()
            workspace.Obstacles:Destroy()

            local Obstacles = Instance.new("Folder")
            Obstacles.Name = "Obstacles"
            Obstacles.Parent = workspace

            Debounce = not Debounce
            print("Done")
        end
    end)		
end)

does this work?

no its still don’t work :frowning:

(Extra Characters)

what exactly doesn’t work?
it doesn’t disable the script anymore so it should run

its run only one time then its dont run

well I found an error and edited it

I know why it only runs once now though, you delete the part so you can’t touch it anymore, the connection is broken

line that breaks it:

Part.Parent:Destroy()

i saw it before and i edit it and its still dont work

it shouldn’t work

I just told you that you deleted the part’s parent, that means both the script and part is deleted, so no code will run at all

ik that but i cloned the RoadModel so its delete the old one and the new one have the script

local Players = game:GetService("Players")
local Part = script.Parent
local Debounce = true

Part.Touched:Connect(function(hit)
    local Player = Players:GetPlayerFromCharacter(hit.Parent)

    if Player and Debounce then
        Debounce = not Debounce

        local Leaderstats = Player:WaitForChild("leaderstats2")
        local Dif = Leaderstats:WaitForChild("Dif")	
        local Clone = workspace.RoadModel:Clone()
        local NewPosition = CFrame.new(workspace.RoadModel.CloneTo.Position)

        Clone:SetPrimaryPartCFrame(NewPosition)
        Clone.Parent = workspace
        Dif.Value -= 1

        task.wait(1)
        Part.Parent:Destroy()
        workspace.Obstacles:Destroy()

        local Obstacles = Instance.new("Folder")
        Obstacles.Name = "Obstacles"
        Obstacles.Parent = workspace

        print("Done")
    end
end)

try this then, I removed the PlayerAdded event

Its work now tysm bro

(More Characters)

local make = function()
	local player = game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats2")
	local Dif = leaderstats:WaitForChild("Dif")	
	local part = script.Parent
	part.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local Clone = game.Workspace.RoadModel:Clone()
			local NewPosition = CFrame.new(game.Workspace.RoadModel.CloneTo.Position)
			Clone:SetPrimaryPartCFrame(NewPosition)
			Clone.Parent = workspace
			Dif.Value = Dif.Value -1	
			script.Disabled = true	
			wait(1)	
			part.Parent:Destroy()
			workspace.Obstacles:Destroy()
			local Obstacles	= Instance.new("Folder", workspace)	
			Obstacles.Name = "Obstacles"
			script.Disabled = false	
			print("Done")		
		end)		
	end)
end

Make sure to put it into a function so you can use it more than once!

If you want to make it repeat constantly, put the function into a loop like this:

while wait() do
	make()
end

You can end the loop by using “break” anywhere inside, which will force it to yeild.
if you want to run this in the background you can also put it inside a coroutine,
Heres some info about them if you’re confused:

no don’t do this, please

they don’t need a loop at all, you didn’t even remove the ".Disabled = boolean" part :man_facepalming:

1 Like

ty for the script but the topic already solved

1 Like

The goal is to make it run constantly (more than one time), please don’t spread negativity, correct people nicely.

The topic are already solved…