when the player sits, a hat will be cloned inside it. When he leaves the seat, the hat needs to be removed. In the above part of cloning the hat within the character is working, but at the time of leaving the seat, it is not removing. Does anyone have any tips?
if script.Parent.Occupant:IsA("Humanoid") then
local humanoid = script.Parent.Occupant.Parent
local player = game.Players:GetPlayerFromCharacter(humanoid)
if player ~= nil then
Hair = ServerStorage.WhiteBiker:Clone()
Hair.Parent = humanoid
end
end
end)
function onChildRemoved()
if Hair ~= nil then
Hair:Remove()
end
end
script.Parent.ChildRemoved:connect(onChildRemoved)
script.Parent.ChildRemoved:connect(onChildRemoved)
Nothing gets removed whe nyou get off the seat, only Occupant changes to nil when someone gets off. You could keep the last player who sat on the seat in a variable, so when Occupant is nil, remove the hat from them
Oh and what @Pokemoncraft5290 mentioned as well, didn’t notice you were using the players service haha
For function childremoved, do onChildRemoved(Weld)
When a player sits on a Seat, it creates a SeatWeld (It is really just a weld but I usually refer it to as SeatWeld). You can use the SeatWeld’s Part1(?) and get the humanoid from Weld Part(1). Once you locate the humanoid from the weld, you can proceed on looking for the character and then do GetPlayerFromCharacter.
Here’s a code sample if you are lost:
function onChildRemoved(Weld)
if Weld:IsA('Weld') and Weld.Name == "SeatWeld" then
local player = game.Players:GetPlayerFromCharacter(Weld.Part1.Parent)
if player then
--do something
end
end
end
Fourth edit: PS: You don’t need GetPropertyChangedSignal, you just need to detect a Weld on child added/removed.
(four edits because Im typing this out in the computer lab haha)
local Hair = nil
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
if script.Parent.Occupant and not Hair then
local Char = script.Parent.Occupant.Parent
if game.Players:GetPlayerFromCharacter(Char) then
Hair = ServerStorage.WhiteBiker:Clone()
Hair.Parent = Char
end
elseif not script.Parent.Occupant and Hair then
Hair:Destroy()
end
end)
the script works. But only when the player sits for the first time (it adds and removes the hat), but when the player sits again, it does not work.
I modified a few things in the script and now it’s working. I appreciate the help. below is the reworked code if anyone wants.
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
if script.Parent.Occupant:IsA("Humanoid") then
local humanoid = script.Parent.Occupant.Parent
local player = game.Players:GetPlayerFromCharacter(humanoid)
if player ~= nil then
Hair = ServerStorage.WhiteBiker:Clone()
Hair.Parent = humanoid
end
end
end)
function onChildRemoved()
if Hair ~= nil then
Hair:Remove()
end
end
script.Parent.ChildRemoved:connect(onChildRemoved)