Removing Tools When Leaving Seat

So I know how to give tools to player when they seat, but how to remove the tools when they leave the seat?

This is my script for giving tools when they seat

local seat = script.Parent
seat:GetPropertyChangedSignal(“Occupant”):Connect(function()
    if seat.Occupant:IsA(“Humanoid”) then
        local humanoid = seat.Occupant.Parent
        local player = game.Players:GetPlayerFromCharacter(humanoid)
        local tool = game:GetService("ServerStorage").Popcorn:Clone()
	tool.Parent = plr.Backpack
    end
end)

So it works for giving tools when they seat, but can someone help me how to make that when they leave the seat, it removes the tools that given to them? Thank you.

1 Like

Have you tried getting into the players backpack and removing the tool?

you should add a variable to the script as “lastoccupant” and go based off of that when the occupant turns nil.

local seat = script.Parent
local lastoccupant

seat:GetPropertyChangedSignal(“Occupant”):Connect(function()
    if seat.Occupant:IsA("Humanoid") then
        local humanoid = seat.Occupant.Parent
        local player = game.Players:GetPlayerFromCharacter(humanoid)
        lastoccupant = player
        local tool = game:GetService("ServerStorage").Popcorn:Clone()
	tool.Parent = player.Backpack
    elseif not seat.Occupant and lastoccupant then
        local tool = lastoccupant.Backpack:FindFirstChild("Popcorn") or lastoccupant.Character:FindFirstChild("Popcorn")
        if tool then
            tool:Destroy()
        end
        lastoccupant = nil
    end
end)

Do you know why I get this error?
This is my script, well is like what you made.

local seat = script.Parent
local lastplr 

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant:IsA("Humanoid") then
		local humanoid = seat.Occupant.Parent
		local plr = game.Players:GetPlayerFromCharacter(humanoid)
		local tool = game:GetService("ServerStorage").Popcorn:Clone()
		tool.Parent = plr.Backpack
		lastplr = plr
	elseif not seat.Occupant and lastplr then
		local tool = lastplr.Backpack:FindFirstChild("Popcorn") or lastplr.Character:FindFirstChild("Popcorn")
		if tool then
			tool:Destroy()
		end
		lastplr = nil
	end
end)

local players = game:GetService("Players")
local server = game:GetService("ServerStorage")
local popcorn = server.Popcorn

local seat = script.Parent

local lastPlayer

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		local humanoid = seat.Occupant
		local character = humanoid.Parent
		local player = players:GetPlayerFromCharacter(character)
		if player then
			lastPlayer = player
			local backpack = player.Backpack
			local characterTool = character:FindFirstChild(popcorn.Name)
			local backpackTool = backpack:FindFirstChild(popcorn.Name)
			
			if characterTool or backpackTool then --Prevents giving out duplicate tools to the same player.
				return
			end
			
			local popcornClone = popcorn:Clone()
			popcornClone.Parent = backpack
		end
	elseif not seat.Occupant then
		if lastPlayer then
			local character = lastPlayer.Character
			local backpack = lastPlayer.Backpack
			
			local characterTool = character:FindFirstChild(popcorn.Name)
			if characterTool then
				characterTool:Destroy()
			end
			
			local backpackTool = backpack:FindFirstChild(popcorn.Name)
			if backpackTool then
				backpackTool:Destroy()
			end
		end
	end
end)

I’ve added the tool removing functionality to your script (when the player’s character leaves the seat) and also made some other improvements/optimisations like preventing duplicates tools from being awarded.

1 Like