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:
1 | local rep = game:GetService( "ReplicatedStorage" ) |
2 | local event = rep:FindFirstChild( "VWPassat" ) |
3 |
4 | script.Parent.MouseButton 1 Click:Connect( function () |
5 | local plr = game.Players.LocalPlayer |
6 | event:FireServer(plr) |
7 | end ) |
if you need more, ask :)
With kind regards
VeoBlack (ExperienceHP)
I'm not too sure how you can determine if the player can only spawn 1 car assuming there are more than 1 types of cars in the game itself. Let's assume that you want them to only be able to spawn it with a timer. You'd use a debounce so that they have to wait awhile before spawning it again.
-The panel that spawns the car will be a part with a SurfaceGui with a text button insert a script:
01 | local debounce = false |
02 | script.Parent.MouseButton 1 Click:connect( function (GetCar) |
03 | if not debounce then |
04 | debounce = true |
05 | local Car = game.ServerStorage [ "Atv Buggy Car" ] |
06 | local clone = Car:clone() |
07 | clone.Parent = workspace |
08 | clone:MakeJoints() |
09 | wait( 10 ) --how long before the player can spawn another car (this applies globally to other players too) |
10 | debounce = false |
11 | end |
12 | end ) |
-Place a script inside the vehicle's driver seat. There will be a timer everytime the player is not in that driver seat when they sit in the seat the timer will restart if they do not sit in time, the car despawns.
01 | local seat = script.Parent |
02 | local vehicle = seat.Parent |
03 | local timer = 0 |
04 | if not seat.Occupant then |
05 | while true do |
06 | wait( 1 ) |
07 | timer = timer + 1 |
08 | if seat.Occupant then |
09 | timer = 0 |
10 | local clone = script:Clone() |
11 | clone.Parent = script.Parent |
12 | script:Destroy() |
13 | end |
14 | if timer = = 10 then --number of seconds player is out of their seat for before car despawns (you can change to any value) |
15 | vehicle:Destroy() |
16 | end |
17 | end |
18 | end |
I know this isnt exactly what you asked for but if you find it useful please do use it.
In order to determine if the player has a car, and prevent spawning of another if so you can do something along the lines of
01 | -- In a server script, NOT a local script. |
02 | game.Players.PlayerAdded:Connect( function (player) -- Detect when player joins |
03 | local CurrentCar = Instance.new( "ObjectValue" ) -- Create a new ObjectValue instance. This will be used to determine if and what car the player has already. |
04 | CurrentCar.Name = "CurrentCar" |
05 | CurrentCar.Parent = player -- Parent ObjectValue to player |
06 | end ) |
07 |
08 | local function SpawnCar(player) |
09 | if player.CurrentCar.Value = = nil then -- Check to see if player has a car already. |
10 | local newcar -- Insert your code here. Newcar is your vehicle model clone that the player can drive |
11 | player.CurrentCar.Value = newcar -- Set players currentcar value to the clone above |
12 | end |
13 | end |
14 |
15 | game.Players.PlayerRemoving:Connect( function (player) -- Detect when player leaves |
16 | if player.CurrentCar.Value ~ = nil then -- If player has a car |
17 | player.CurrentCar.Value:Destroy() -- Destroy it |
18 | end |
19 | end ) |