Seat Tools Problem

So I have a script and it can give the tools but when try to remove the tools when player leave the seat, it gives me this error. It makes me feel more weird when it gives me error only when a player leaving their seat, but when they get to seat it’s working, it gives tools. If someone can help me, please help me, thank you.

And this is the script

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)
1 Like

seat.Occupant will return nil if there is no occupant, thus nil:IsA("") will be an attempt to index nil.
Remember the Occupant signal will fire on both entry and exit, this sounds like the error occurs when someone jumps out.

You could address this by checking first if seat.Occupant exists, if not, someone just left the seat and you should skip the tool handling.

1 Like

Yes, but how to make also if someone jump out?

Try adding an ObjectValue to the seat. On the event, if seat.Occupant exists, set the ObjectValue’s .Value to it. If seat.Occupant doesn’t exist, see if the ObjectValue exists. If it does, that’s the person who was just sitting there, so you have an active reference to them.

if seat.Occupant ~= nil and seat.Occupant:IsA("Humanoid") then
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.

I already responded to the other thread but I’ll post the same reply here too.