Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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:

  1. 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?

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

  3. 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)

if you need more, ask :)

With kind regards

VeoBlack (ExperienceHP)

2 answers

Log in to vote
0
Answered by 4 years ago

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:

local debounce = false
script.Parent.MouseButton1Click:connect(function(GetCar)
if not debounce then
debounce = true
        local Car = game.ServerStorage["Atv Buggy Car"]
        local clone = Car:clone()
        clone.Parent = workspace
        clone:MakeJoints()
wait(10) --how long before the player can spawn another car (this applies globally to other players too)
debounce = false
end
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.

local seat = script.Parent 
local vehicle = seat.Parent 
local timer = 0 
if not seat.Occupant then 
while true do
wait(1)
timer = timer + 1
if seat.Occupant then 
timer = 0 
local clone = script:Clone() 
clone.Parent = script.Parent 
script:Destroy() 
end
if timer == 10 then --number of seconds player is out of their seat for before car despawns (you can change to any value)
vehicle:Destroy() 
end
end
end

I know this isnt exactly what you asked for but if you find it useful please do use it.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

-- In a server script, NOT a local script.
game.Players.PlayerAdded:Connect(function(player) -- Detect when player joins
    local CurrentCar = Instance.new("ObjectValue") -- Create a new ObjectValue instance. This will be used to determine if and what car the player has already.
    CurrentCar.Name = "CurrentCar"
    CurrentCar.Parent = player -- Parent ObjectValue to player
end)

local function SpawnCar(player)
    if player.CurrentCar.Value == nil then -- Check to see if player has a car already.
        local newcar -- Insert your code here. Newcar is your vehicle model clone that the player can drive
        player.CurrentCar.Value = newcar -- Set players currentcar value to the clone above
    end
end

game.Players.PlayerRemoving:Connect(function(player) -- Detect when player leaves
    if player.CurrentCar.Value ~= nil then -- If player has a car
        player.CurrentCar.Value:Destroy() -- Destroy it
    end
end)

Answer this question