Hello.
I have a question regarding a pickup script, but first, allow me to define pickup in this context.
Pickup, in this case, means if the script finds two cars with the same ‘PickupString’ (which is set to the player who spawned the car) value, it will delete the car.
I don’t have a perfect idea of how to achieve this, but here is my code.
wait(2)
print("Script Running")
local owners = {
}
print("Set Owners")
local Folder = game.Workspace.SpawnedCars
print("Set Folder")
for i, object in pairs(Folder:GetChildren()) do
print("Started Loop")
if table.find(owners, object.PickupString.Value) == nil then
print("Running Table 'if'")
owners[i] = object.PickupString.Value
print("Running Table 'if' 1")
else
print("Abt To Destroy")
object:Destroy()
print("Destroyed")
end
end
Please let me know if you have an idea!
The code you have shown here should work. However, I don’t think you should have such a script in your game, as it means that it’s possible for a player to spawn two cars at once. Instead of adding this script, you should just add a check at your car spawning script to make sure the player doesn’t already have a car spawned. If they do, delete the existing car.
Alright. And how might I do that?
In your server script for spawning cars, before spawning a car, check to make sure the player doesn’t already have a car spawned. There are many ways to do this.
- Add an ObjectValue to each player, value is their current spawned car
- All cars spawn in a Cars folder, check folder for car with player id
Thanks, it all seems to be working except for one thing. I am not sure how to find the first car that the player spawned, ideas?
Depends on which method you chose.
You can iterate through all cars and look for the value with the player ID.
Or you can get the value of the PlayerCar objectvalue (that value is their spawned car).
But it will break when multiple people spawn the same car.
Multiple people cannot spawn the same car (the same model, yes, but each car will be unique. Add an StringValue with the name of the player spawning inside each spawned car)
I already have that.
I have a start, but am not sure…
for _, Obj in pairs(game.Workspace.SpawnedCars:GetDescendants()) do
end