Clone() not working correctly

Hello everyone! I have this script, it is a car respawner, it works well expect for the fact that when it respawns, the car always respawns in the direction of where the previous one is, this is very annoying since im trying to make it spawn inside the parking lines. Does anyone know why it is doing this? It should be cloning the one in replicatedstorage. Im trying to make it clone straight.

Video is an example of what happens.
robloxapp-20210424-1901101.wmv (2.4 MB)

Thanks for your help,
Real_FoxBlox

RS = false

local button = script.Parent.Parent.Button
local car = game.ReplicatedStorage.PickupTruck

button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
if RS == false then
RS = true
print(“Car respawning.”)
button.BrickColor = BrickColor.new(“Bright red”)
local clonecar = car:Clone()
clonecar:Destroy()
clonecar = script.Parent.Parent
clonecar:MoveTo(Vector3.new(503.5, 2.15, -501))
button.Position = Vector3.new(493.5, 2.15, -525.5)
wait(5)
button.BrickColor = BrickColor.new(“Bright green”)
RS = false
else
return
end
end
end)

1 Like

That is your issue. You are cloning it, then immediately destroying it. Clone is working.

Edit: I tackled the wrong problem, I’ll scan through your code again, sorry. Just as a note, you can put three ``` above and below your code, to fomat it, like so:

RS = false

local button = script.Parent.Parent.Button
local car = game.ReplicatedStorage.PickupTruck

button.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild(“Humanoid”) then
        if RS == false then
            RS = true
            print(“Car respawning.”)
            button.BrickColor = BrickColor.new(“Bright red”)
            local clonecar = car:Clone()
            clonecar:Destroy()
            clonecar = script.Parent.Parent
            clonecar:MoveTo(Vector3.new(503.5, 2.15, -501))
            button.Position = Vector3.new(493.5, 2.15, -525.5)
            wait(5)
            button.BrickColor = BrickColor.new(“Bright green”)
            RS = false
        else
            return
        end
    end
end)
2 Likes

Your script isn’t doing this. Specifically, this part:

You are trying to clone the car in ReplicatedStorage, but then you destroy it for whatever reason, and instead end up doing this after deleting it:

clonecar = script.Parent.Parent

if you are trying to set the parent of the model, simply change that to

clonecar .Parent = script.Parent.Parent

and remove the line saying

clonecar:Destroy()

Your final script should look like:

RS = false

local button = script.Parent.Parent.Button
local car = game.ReplicatedStorage.PickupTruck

button.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild(“Humanoid”) then
        if RS == false then
            RS = true
            print(“Car respawning.”)
            button.BrickColor = BrickColor.new(“Bright red”)
            local clonecar = car:Clone()
            clonecar.Parent = script.Parent.Parent
            clonecar:MoveTo(Vector3.new(503.5, 2.15, -501))
            button.Position = Vector3.new(493.5, 2.15, -525.5)
            wait(5)
            button.BrickColor = BrickColor.new(“Bright green”)
            RS = false
        else
            return
        end
    end
end)

Just let me know if that still doesn’t work, thanks!

1 Like