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

how do I make the vehicle disappear if the player leaves the vehicle for a few seconds?

Asked by 3 years ago
Edited 3 years ago

hello I want to ask, how do I make the vehicle disappear if the player leaves the vehicle for a few seconds and the vehicle reappears back to its original place?

1 answer

Log in to vote
0
Answered by 3 years ago

Script 1. When you initially spawn the vehicle add it's instance to an array.

Script 2. Have a script withing the vehicle detect when a player leaves the seat and then use :Destroy() on the vehicle.

Script 1. In the first script, if array.Length != 0 then... Loop every few seconds to see if the vehicle still exists. If it doesn't then someone has left the seat and then vehicle has been destroyed so you should spawn a new one!

Writing on mobile and out of practise so sorry if there are bugs:

//Script 1 - inside an invisible object which is at the position you want to spawn the vehicle.
//This object should be parented to the same model which contains your vehicle

local replicatedStorage = game.GetService("ReplicatedStorage")

local containerModel = script.parent.parent
local spawnLocation = script.parent //this is the invisible block
local storedVehicleModel = replicatedStorage.vehicleModel:clone() //where vehicleModel is your car and is stored in replicatedStorage

local array = {}

local function SpawnNew()
storedVehicleModel.parent = containerModel //puts the car in workspace
storedVehicleModel.position = spawnLocation.position //puts the car in the spawn position
array.Push
array.insert(storedVehicleModel) //adds the object to a list
end

While true do //while the game is running check if the vehicle exists every 5 seconds, if it doesn't, spawn one.
wait(5)
if not array[0] then //if there isn't a car, do function to spawn one
//I'm actually not sure if destroying the car from script 2 will make this return as false so give it a test!
SpawnNew ()
end
end
//Script 2 - inside the vehicles seat

local seat = script.parent
local vehicleModel = script.FindFirstAnscestorOfClass("Model")

//This next line is confusing but all it does is check the occupant property for change and when it's triggered do something
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
If seat.Occupant == nil then
vehicleModel:Destroy() //destroys the car when nobody is in it
end
end)

Hopefully this works, if you've got questions or issues let me know or check the wiki!

Ad

Answer this question