Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Car doesn't spawn in after it has been destroyed?

I'm having an error where my car doesn't spawn back in after it has been destoryed. Not sure why, however here is a gif for my detail on what I'm trying to explain: Link

Script I'm working on:

local player = game.Players.LocalPlayer.UserId -- localplayer's id
local playername = game.Players.LocalPlayer.Name -- localplayer's name
local car = game.ReplicatedStorage.Cars.PoliceCar -- location of car
local clonedcar = car:Clone()-- clones car

function SpawnCar()
	clonedcar.Name = player -- changes name of car to localplayer's id
	clonedcar.VehicleSeat.Owner.Name = playername -- changes the owner of the car to the player's name
	clonedcar.Parent = game.Workspace -- moves car to workspace
end -- everything above here works fine.

script.Parent.MouseButton1Click:connect(function() -- on button click
    if game.Workspace:FindFirstChild(player) == nil then -- if car doesn't exist then
        SpawnCar() -- spawn new car
	else
		game.Workspace:FindFirstChild(player):Destroy()
		wait(1)
		SpawnCar()
    end
end)

If you need more information, please contact me. :)

1 comment

0

Try moving the clonedcar variable into the function SpawnCar. That way it will clone a new copy each time the function is called. I know it sounds odd and picky, but think of it as a new instance. Hope it helps, and if it doesn't just contact me!

1 answer

Simple tweak, but should fix your issue.

local player = game.Players.LocalPlayer.UserId -- localplayer's id
local playername = game.Players.LocalPlayer.Name -- localplayer's name
local car = game.ReplicatedStorage.Cars.PoliceCar -- location of car
local clonedcar = car:Clone()-- clones car

function SpawnCar()
    -----
    clonedcar = clonedcar:Clone() -- Stores the original car as a backup then loads it
    -----
    clonedcar.Name = player -- changes name of car to localplayer's id
    clonedcar.VehicleSeat.Owner.Name = playername -- changes the owner of the car to the player's name
    clonedcar.Parent = game.Workspace -- moves car to workspace
end -- everything above here works fine.

script.Parent.MouseButton1Click:connect(function() -- on button click
    if game.Workspace:FindFirstChild(player) == nil then -- if car doesn't exist then
        SpawnCar() -- spawn new car
    else
        game.Workspace:FindFirstChild(player):Destroy()
        wait(1)
        SpawnCar()
    end
end)

1 comment

0

Still the same issue, all you did is just move the local clonedcar into the function. I'm pretty sure theirs a problem with the 11th line

This discussion is preserved as it was. New answers, comments, and votes are closed.