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)
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)
add this script where it summons the car, then add a script like this
game.ServerStorage.youritemhere:Destroy()