How to delete car when player leaves the game

I need help creating a script I’m not sure how to do this to make sure my car gets deleted but also works with my pickup script. I’m new to scripting but this is kind of what I’ve came up with but I’m sure it’s close but still wrong lol

I really need my cars to delete if a player leaves or it will make my Wisconsin rp community game clog up with cars and no players to delete them.

I have search and couldn’t come up with anything close to what I’m looking for I could only find if the player gets out of the car, and I only want it to delete if the player leaves the game.

Then on top of it is this a script that goes in workspace or do I put it in each individual car seat. Thank you

seat = script.Parent

game.Players.PlayerRemoving:Connect(function(child)
	if child.Name == "SeatWeld" then 	
	   local human = child.Part1.Parent:findFirstChild("Humanoid")
	   if human ~= nil then
		seat.Parent.Name = human.Parent.Name.."Car"	
		seat:Destroy()
		end
	end
end)

This is the custom pickup script I’m using so my car spawner deletes my cars without this one it won’t delete my cars when you press the delete button so I’m thinking it has to work together but I’m not 100% sure.

seat = script.Parent
s = script

function onChildAdded(child)
	if child.Name == "SeatWeld" then
		local human = child.part1.Parent:findFirstChild("Humanoid") 
		if (human ~= nil) then
			print("Human IN")
			seat.SirenControl.Control.CarName.Value = human.Parent.Name.."Car"
			seat.Parent.Name = human.Parent.Name.."Car"
			s.Parent.SirenControl:clone().Parent = game.Players:findFirstChild(human.Parent.Name).PlayerGui
			s.Parent.RDetector:clone().Parent = game.Players:findFirstChild(human.Parent.Name).PlayerGui
			seat.Start:Play()
			wait(1)
			seat.Start:Stop()
			seat.Engine:Play()
		end
	end
end

function onChildRemoved(child)
	if (child.Name == "SeatWeld") then	
		local human = child.part1.Parent:findFirstChild("Humanoid") 
		if (human ~= nil) then
			print("Human OUT")
			seat.SirenControl.Control.CarName.Value = human.Parent.Name.."Car"
			seat.Parent.Name = human.Parent.Name.."Car"
			game.Players:findFirstChild(human.Parent.Name).PlayerGui.SirenControl:remove()
			game.Players:findFirstChild(human.Parent.Name).PlayerGui.RDetector:remove()
			seat.Engine:Stop()
		end
	end
end


script.Parent.ChildAdded:connect(onChildAdded)
script.Parent.ChildRemoved:connect(onChildRemoved)

Like I said not to sure how to go about this so they work together or if it’s something that can be done as a script by itself

1 Like

You’d be better listening to changes against the Seat.Occupant property, which will be the sitting player’s humanoid, since this is less likely to erroneously trigger your event (and also requires less conditions)

Seat:GetPropertyChangedSignal("Occupant"):Connect(function(human: Humanoid?)
  if human then
    print("someone is now sitting in the seat")
  else
    print("there is no longer anyone in the seat")
  end
end)
1 Like