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

How to improve on AlvinBlox's Car spawning tutorial?

So I have watched AlvinBlox's tutorial on Car spawning and it works fine but I have one question

how do I make it where a player can only spawn 1 car then his previous car gets deleted?

All the scripts are in his video about it im to lazy to pull up all the scripts and the hierarchy please help

2 answers

The general idea is to check if the car exists. There are a few ways you can do this, however, I would recommend doing something like this:

local result, err = pcall(function() -- catches all game-breaking errors
	return car and car.Parent
end)
if result then
	if err == true then
		-- car exists
		car:Destroy()
	else
		-- car does not exist or parent is nil
	end
else
	-- car does not exist or parent is nil
end

-- Car spawn logic

4 comments

0

and i put this in my leaderstats correct but right above the last end?

1

You did not provide any code, so I am not sure where you would want to place this within your script. I provided a comment at the end which should help with placement.

0

this is where the car spawns

0

i provided it by awnserwing

local function onPlayerJoin(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 22000
	money.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)
	return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
end

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
	local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
	car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
	car.Parent = workspace
	car:MakeJoints()
	car.Name = player.Name.."'s "..NameOfCar
end)

1 comment

0

Fixed Codeblock.

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