Hello! I am Impulse55467 and I am an animator that has started to learn how to script. After looking at some tutorials and other resources, I have made a working car and I have made it so that you can spawn and despawn the car via buttons.
Now, this is all and good, but with the way my Button Scripts are set up, there are no limits to how many cars you can spawn with the button and since the Spawn Button just clones the car from Server Storage to a folder in the Workspace, people can freely delete other people’s cars.
To combat this, I implemented a script so that when a player joins, it creates Folder in the Workspace that is unique to them. But the problem that I am having now is that I can’t find a way to have it so when a player presses the Spawn Button, the car goes into their unique folder.
How should I approach this? Is the system that I currently have good, or should I just rewrite it entirely? I’ll leave some pictures and videos down bellow of what I currently have. Thanks!
(Video of the spawning system)
(Spawning Script)

(Despawning Script)

(Folder Creation Script)

(How everything is ordered)

I’d say this is a good system so far. Or maybe you could just have the button disabled when they have more then one car spawn?
That is a good idea, but if you look at my despawning script, the script finds the first child of the Folder in the Workspace and then sends it back to Server Storage. So, if anyone in the game were to press the DeleteCar button, it would delete the first car in the folder, instead of their car.
Maybe instead then, go ahead and make a folder in the player object. Don’t store the cars here, instead store an ObjectValue inside that and access the ObjectValue and then send that back to ServerStorage.
Maybe thats a better system?
That idea could work, I’ll give it a go. Thanks!
I don’t think your folder idea is bad.
I would maybe suggest using a table instead, though. You can use it like a folder, except you can map players directly to cars:
local cars = {}
-- get player object e.g. from a RemoteEvent call
cars[player] = game.ServerStorage.CarTemplate:Clone() -- or whatever
-- and to remove later:
cars[player]:Destroy()
cars[player] = nil
Either folder or table is fine, though.
In terms of actually getting/deleting cars, create two RemoteEvents in ReplicatedStorage. One for spawning, one for deleting.
On the server, listen for either of those and attach new cars or delete old ones as required. For example the spawning might look like:
local spawnRemote = game.ReplicatedStorage.SpawnCarRemoteEvent
local cars = {}
spawnRemote.OnServerEvent:Connect(function(player)
if cars[player] then
cars[player]:Destroy() -- delete old car
end
cars[player] = -- clone car somehow, parent to workspace, etc.
end
Then all the client needs to do is call game.ReplicatedStorage:WaitForChild("SpawnCarRemoteEvent"):FireServer()
, and the server will create a car for it and keep track of it.
There’s some details to add here (besides deleting cars), like making sure you clean up cars when players leave, etc.