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.
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!
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.
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)