Problem with clones and despawning if no longer in use

Hello! I have a Plane and this script on the platform it appears.
image

Basically it makes a clone of the plane if someone takes it a bit far away. I want it so the Cloned Planes also get deleted if the Seat is empty for a while (that way i know nobody is using the cloned plane anymore)

here is the script:

local current = script.Parent.Parent.Parent.Parent:FindFirstChild("PlaneFlying")
local copy = current:Clone()

function add()
	if current.Parent then
		current.Parent = game.Workspace
	end
	local c = copy:Clone()
	c.Parent = script.Parent.Parent
	c:makeJoints()
	current = c
end

while true do 
	wait(5)
	if (script.Parent.Position - current.VehicleSeat.Position).magnitude > 20 then
		add()
	end
end

How could i change this so the cloned planes despawn after no longer being used

1 Like

i would try using the plane’s seat as a way to destory the plane’s Captain if not seated

--try something like this 
while wait(5) do
	local character = script.Parent
	local humanoid = character:WaitForChild("Humanoid")
	local plane = script.Parent.Parent.Model
	local function onSeated(isSeated, seat)
		if isSeated then
			print("Everything is good")
		else
			plane:Destory() --add you're own model here 

		end
	end

	humanoid.Seated:Connect(onSeated)
end
1 Like

where would i put this code? inside one of the existing scripts or a new one and if so, where or which? thanks for the reply

1 Like

This will create a new function every 5 seconds that will fire when a player sits, then if a player does sit, it’s going to only delete the plane if a player isn’t sitting after a player sits.

Instead, have one main script that will start a timer for each plane when a player gets up, then delete the plane after the timer is up.

2 Likes

Thanks! Finally got it to work with both replies showing the right way to look for :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.