Now I’m working on expanding that game. So I added some zombies. And that, of course, made it better.
Then I got stuck. The tutorial includes a car that a player spawns into the seat. What I would like to have instead is one car per player. So every time a new player joins the game they get their own car. And when they die, their car get deleted as well.
I don’t expect anyone to code this for me. I just need some general guidelines. I’m new to Roblox, but I’m learning quickly and I’ve gone through all the tutorials. I found some YouTube videos that were similar but they were for the purchase of a car. So I’m still stuck and don’t know where to look next…
You can create a new Bool-Value with a Local Script and set it to true or false if a player spawned a car. If its true or false the player cannot spawn a car anymore.
And then like Aguia said you can use Humanoid.Died Event and make the Bool true or false so the player can spawn a car.
Also I would suggest you put your car models inside the ReplicatedStorage and clone them. Like Obsessed said.
What I’ve done here is given guidelines and hints, as you requested, and then put examples of written code behind a blur which you can click if you want to see it. Also, you can click events I say in descriptions to see their info!!
For a starting point, use the PlayerAdded event to detect when a player joins the game. Then, clone the vehicle (which you should have in ServerStorage), parent it to Workspace, and name the vehicle model accordingly.
For example, a ServerScript in ServerScriptService…
local SS = game:GetService("ServerStorage")
local car = SS.Car -- path to car model
game.Players.PlayerAdded:Connect(function(player) -- detect player joining
local cloned = car:Clone() -- clone car
cloned.Name = player.Name .. "’s Vehicle" -- eg jackfrojpg’s Vehicle
cloned.Parent = game.Workspace -- parent vehicle to Workspace
end)
Now, deleting the car when they die. You can use Humaniod.Died to detect when they die and then delete their current car, and spawn a new one.
For example, a ServerScript in ServerScriptService…
local SS = game:GetService("ServerStorage")
local car = SS.Car -- path to car model
game.Players.PlayerAdded:Connect(function(player) -- detect when player joins
player.CharacterAdded:Connect(function(char) -- detect when character spawns
char:WaitForChild("Humanoid").Died:Connect(function() -- detect when character’s humanoid dies
if game.Workspace:FindFirstChild(player.Name .. "‘s Vehicle") then -- check if player has a car
game.Workspace:FindFirstChild(player.Name .. "’s Vehicle"):Destroy() -- destroy car if it exists
end
local cloned = car:Clone() -- clone car
cloned.Name = player.Name .. "’s Vehicle" -- eg jackfrojpg’s Vehicle
cloned.Parent = game.Workspace -- parent car to Workspace
end)
end)
end)
Finally, let’s delete the car when the player leaves the game. You can use PlayerRemoving event to detect when a player leaves the game, and then destroy their vehicle if it exists.
game.Players.PlayerRemoving:Connect(function(player) -- detect just before player leaves
if game.Workspace:FindFirstChild(player.Name .. "‘s Vehicle") then -- check if they have a car
game.Workspace:FindFirstChild(player.Name .. "’s Vehicle"):Destroy() -- destroy their car if it exists
end
end)
I hope this helps you, and I wish you the very best of luck with your project/game!!
As soon as I read this I was sold. You provided exactly what I was hoping to get. As did all the other replies. Your tips and advice made total sense to me because I’ve done a bunch of tutorials along similar lines. So thank you all.
I had to make a minor change to the script that came with the tutorial, otherwise the rest of the code turned out to be this simple:
--[[
Manage the cloning of a car as a player enters the game.
Remove the car when the player leaves the game.
--]]
local function onPlayerAdded(player)
local clone = game:GetService("ServerStorage").Car:Clone()
clone.Parent = workspace
end
local function onPlayerRemoving(player)
if workspace:FindFirstChild("Car") then
workspace:FindFirstChild("Car"):Destroy()
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
But note, you are deleting anything in workspace called Car.
This is an issue unless you are only allowing 1 player in a server. Why? It might not end up deleting the specific player’s car when they leave. Everyone’s car in the game will be named “Car”, so it will delete the first one of those it comes across.
That is why I named the car to player.Name .. "'s Vehicle" so you would only delete the specific player in question’s car, but if you are only having 1 player servers, that shouldn’t be an issue.
Try checking to make sure that the car models were named correctly in relation to the player’s username, and also ensure that you’re sitting the players in the correct car.