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

Car spawner, limit and delete the car after they left? ^

Hey,

I am beginning to despair... as I am getting nowhere. I have a car spawner but players can spawn an infinite number of cars at once and when players leave the game the car remains there. and i can't manage to make a delete button for the cars .-.

now my question:

how do I make it so that player can spawn only 1 car, but if it then deletes the car it can spawn 1 again?

how do I make it that the car automatically despawns when the player leavs from the game?

How do I make a delete button for the current car he owns?

sry for the many questions but I've been trying for 1 month to do this.

Here is my car spawner script:

local rep = game:GetService("ReplicatedStorage")
local event = rep:FindFirstChild("VWPassat")

script.Parent.MouseButton1Click:Connect(function()
	local plr = game.Players.LocalPlayer
	event:FireServer(plr)
end)

local rep = game:GetService("ReplicatedStorage")
local event = rep:FindFirstChild("VWPassat") --your RemoteEvent

local function CreateCar(plr)
	if plr.TeamColor  == BrickColor.new("Really blue")-- your team color
	then
		local clone = game.ServerStorage:WaitForChild("VWPassat"):Clone()
		clone.Parent = game.Workspace
		clone:MakeJoints()
	else print("Not on the correct team")-- if you want an error message or something do it here
	end
end

event.OnServerEvent:Connect(CreateCar)

~VeoBlack (ExperienceHP)

2 answers

✓ Accepted answer

I would create a table which would store current spawned car of the player, with that you can check if gamer has spawned car or if he does not. For removing car when player leaves you can use Players.PlayerRemoving event which fires when gamer is leaving.

First of all, when you fire remote event to the server, you don't need to specify the LocalPlayer as it automatically fires as the first parameter, also FindFirstChild does not server purpose in it so you should remove it.

local rep = game:GetService("ReplicatedStorage")
local event = rep.VWPassat

script.Parent.MouseButton1Click:Connect(function()
	event:FireServer()
end)

In the server script you should implement the structure because it is best way to stop exploiters that want to cheat.

local rep = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local event = rep.VWPassat -- your RemoteEvent

-- Cars owned by players will be stored in here
local Cars = {}

local function CreateCar(plr)
	-- This will stop the function and return if player has been found
	-- in the table with cars, means he has car spawned already
	if Cars[plr.UserId] then
		print("Gamer has already car spawned")
		return
	end
	
	if plr.TeamColor == BrickColor.new("Really blue") -- your team color
	then
		local clone = ServerStorage.VWPassat:Clone()
		clone.Parent = workspace
		
		-- I don't know what this does but i assume it works for you
		-- so i won't touch it
		clone:MakeJoints()
		
		-- This will save variable of the gamer to the cars table so next time
		-- he tries to spawn a cat it will see that he has already spawned it
		Cars[plr.UserId] = clone
	else
		print("Not on the correct team") -- if you want an error message or something do it here
	end
end

local function RemoveCar(Client)
	if Client then
		local Car = Cars[Client.UserId]
			
		if Car then
			Cars[Client.UserId] = nil
			Car:Destroy()
		end
	end
end

local function OnClientRemoving(Client)
	-- When player leaves, this will check if gamer has car spawned in the
	-- table and if he does, it will destroy the car and remove it from table
	RemoveCar(Client)
end

Players.PlayerRemoving:Connect(OnClientRemoving)
event.OnServerEvent:Connect(CreateCar)

Make sure not to use WaitForChild on the server as the Instances are always replicated there so indexing is just enough, if you are not sure that Instance is there then use it no problem.

Now you have table Cars which is list of all cars that are spawned, if you want to destroy the car on click, you will need to fire remote event from the client and on the server and then call the RemoveCar function which will find the car in the table and destroy it. Example

local function OnDestroyCar(Client)
	RemoveCar(Client)
end

-- Example RemoteEvent named DestroyCar
DestroyCar.OnServerEvent:Connect(OnDestroyCar)

3 comments

0

Thank you very much! it worked :D a small question still, since I just have only 1 vehicle what I can spawn, how is it with if I also have other vehicles? is then the current car deleted and spawned for the new? oh yes this line code "if plr.TeamColor == BrickColor.new("Really blue") -- your team color then local clone2 is there so that only this group can access or spawn ^^

0

You would have to user PlayerAdded event which fires when you join the game, then you would create empty table of player's cars and store all of them there, here is example https://pastebin.com/TXLhG1nV. In this case when you are firing remote event to Create/Destroy the car you need to specify the second parameter which is the brand of the car, so you can have multiple of them, "Yellow", "Blue"..

1

Hey, ok thanks, only I didn't understand it correctly, where do I have to insert that? (can we write about Discord? its better ^^ if the answer is yes here my username: VeoBlack#9999

add this script where it summons the car, then add a script like this

game.ServerStorage.youritemhere:Destroy()

2 comments

0

Thanks! i think this is the delete button?

0

-1, don't spoonfeed

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