How do I unequip a tool once the player has left a seat?

I want to unequip a tool from a player once they jump out of a seat.

This is my code so far:

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
	if Player then
		Player.Team = game:GetService("Teams")[Team]
		seat.Occupant:EquipTool(Tool)
	elseif not seat.Occupant then
		Player:UnequipTools()
	end
end)

I am getting the error: attempt to index nil with 'Parent' - Server - Script:7

I am not that good with coding so if someone could help me I would be very happy.

2 Likes
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
	if Player then
		Player.Team = game:GetService("Teams")[Team]
		seat.Occupant:EquipTool(Tool)
	elseif not seat.Occupant then
		Player.Character:FindFirstChild("Humanoid"):UnequipTools()
	end
end)

UnequipTools() is a function that is used on the humanoid listed in the documentation. Hope this helps!

1 Like

Thank you for this, but even after making these changes I still seem to be getting the same error and the tool is not unequipping from the character.

I’m also not sure what change this is making as seat.occupant (what player is) as far as I’m aware is a humanoid anyway.

1 Like

Oh my fault I see something now. before you create the Player variable you should create a new variable like so

local character = seat.Occupant:FindFirstAncestorWhichIsA("Model")

then do your

local Player = Players:GetPlayerFromCharacter(character)

let me know if this works for you

1 Like

also the seat.occupant is nil when the player leaves the seat but thats not your issue

1 Like
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if seat.Occupant == nil then return end
	local Player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
	if Player then
		Player.Team = game:GetService("Teams")[Team]
		seat.Occupant:EquipTool(Tool)
	elseif not seat.Occupant then
		Player:UnequipTools()
	end
end)

By just simply checking if theres an actual occupant, your problem should be fixed.

1 Like

Now I am still getting the error:

Workspace.Judge.Seat.Script:8: attempt to index nil with 'FindFirstAncestorWhichIsA'

Line 8 is: local char = seat.Occupant:FindFirstAncestorWhichIsA("Model")

This error only runs when the character leaves the seat.
It’s trying to get the player as it leaves the seat but because it’s they’ve left, it’s nil

1 Like

The solution I provided will fix this issue.

1 Like

This fixes the error, but not the problem that I had originally. Which is to unequip the tool once they’ve left the seat.

1 Like

Sorry for that. Ill work on a replacement, give me a second.

1 Like

this is when you could use the
Player.Character:FindFirstChild("Humanoid"):UnequipTools()
to replace your Player:UnequipTools()

2 Likes

This should work:

local last_player

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Player
	if seat.Occupant ~= nil then
		Player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
		if Player then
			Player.Team = game:GetService("Teams")[Team]
			seat.Occupant:EquipTool(Tool)
			last_player = Player.Character
		end
		return
	end
	
	if last_player ~= nil then
       last_player.Humanoid:UnequipTools()
    end
end)
5 Likes

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