Script works only one time

Viewed 49

Summarize the problem I have a script that is in the server storage. When a block is clicked, the script gets cloned and assigned as child to the block to make the script start. At the end of the script it gets destroyed but rest in the storage. It works perfectly the first time it gets cloned and then destroyed. But when I click the block again to make the script clone and work, it gives me problems. It gets cloned but it doesn't start.

Describe what you’ve tried I've tried searching around for someone with my same problem, sadly i couldn't find it.

When appropriate, show some code

part of the code that clone the script

The ClickDetector is located in the workspace

local Touch = game.ServerStorage.TakeToTable:Clone()
Touch.Parent = ClickDetector

code that doesn't start the second time

local ClickDetect = script.Parent
local Clients = {ClickDetect:FindFirstChild('Client1'), ClickDetect:FindFirstChild('Client2'), ClickDetect:FindFirstChild('Client3'),
    ClickDetect:FindFirstChild('Client4')}
    
local Tables = {
    game.Workspace.Tables.Table1, game.Workspace.Tables.Table2,game.Workspace.Tables.Table3,game.Workspace.Tables.Table4,
    game.Workspace.Tables.Table5,game.Workspace.Tables.Table6,game.Workspace.Tables.Table7,game.Workspace.Tables.Table8,
    game.Workspace.Tables.Table9,game.Workspace.Tables.Table10,game.Workspace.Tables.Table11,game.Workspace.Tables.Table12,
    game.Workspace.Tables.Table13,game.Workspace.Tables.Table14,game.Workspace.Tables.Table15,game.Workspace.Tables.Table16,
    game.Workspace.Tables.Table17,game.Workspace.Tables.Table18,game.Workspace.Tables.Table19}

ClickDetect.MouseClick:Connect(function()
    for index = 1,#Tables do
        local ClickDetectTables = Instance.new('ClickDetector')
        ClickDetectTables.Parent = Tables[index]
    end
    for index = 1,#Tables do
        Tables[index].ClickDetector.MouseClick:Connect(function()
            if #Clients == 1 or #Clients == 2 then
                local LetsGo = game.ServerStorage.LetsWalk:Clone()
                LetsGo.Parent = script.Parent
                local Table = Instance.new('Frame')
                Table.Parent = ClickDetect.TakeToTable
                Table.Name = "Table" .. index
                for index = 1, #Tables do
                    Tables[index].ClickDetector:Destroy()
                end
                local PathToTable = Tables[index].Path:Clone()
                PathToTable.Parent = script.Parent
                PathToTable.Name = "PathToTable"
            end
            
            if #Clients == 3 or #Clients == 4 then
            local Seats = {Tables[index]:FindFirstChild('Seat1'), Tables[index]:FindFirstChild('Seat2'),
                Tables[index]:FindFirstChild('Seat3'),
                Tables[index]:FindFirstChild('Seat4')}
                if #Seats == 3 or #Seats == 4 then
                    local LetsGo = game.ServerStorage.LetsWalk:Clone()
                    LetsGo.Parent = script.Parent
                    local Table = Instance.new('Frame')
                    Table.Parent = ClickDetect.TakeToTable
                    Table.Name = "Table" .. index
                    for index = 1, #Tables do
                        Tables[index].ClickDetector:Destroy()
                    end
                    local PathToTable = Tables[index].Path:Clone()
                    PathToTable.Parent = script.Parent
                    PathToTable.Name = "PathToTable"
                end
            end 
            end)
    end
    
end)
1 Answers

The issue is likely that ClickDetector is an undefined value in your first code block.

Because you don't have a line that locates the ClickDetector in the Workspace, the ClickDetector variable evaluates as nil. And when you set the Parent property to nil, it removes it from the game.

So the fix is simple, just define the variable with the path to the object. (I'm assuming the object is named ClickDetector)

-- find the detector in the Workspace...
local ClickDetector = game.Workspace.ClickDetector

-- clone the script to the detector...
local Touch = game.ServerStorage.TakeToTable:Clone()
Touch.Parent = ClickDetector
Related