I'm trying to make is a script that will give the player a tool when you sit on a certain seat. Then, when the player jumps off the seat, the tool should be gone from their backpack.
The code below is what I'm using and I cannot get it to work.
function onChildAdded(child) if child.Name == "SeatWeld" then if child.Part1.Name == "Torso" then player = game.Players:GetPlayerFromCharacter(child.Part1.Parent) if player ~= nil then tool = game.ReplicatedStorage.MachineShoulder:Clone() tool.Parent = player.Backpack end end end end function onChildRemoved() if tool ~= nil then tool:Remove() end end script.Parent.ChildAdded:connect(onChildAdded) script.Parent.ChildRemoved:connect(onChildRemoved)
Can anyone explain to me why is this not working?
Thank you.
Also I am getting no sort of error when sitting on the seat. Just some extra information for you.
Try this out. There are some more modern ways of detecting seat changes and who did so. You can then get that info to give or remove the tool all in one function. Dont forget to check the players backpack and the character itself for tools, because when you equip a tool it gets moved from Backpack to Character until unequipped, so lets make sure we dont give duplicates. Your code wasn't defining tool properly, and so it wouldnt give it.
local seat = workspace.Seat -- Define seat local ToolToGive = game.ReplicatedStorage.MachineShoulder -- Define tool to give local player -- Define player for later use local function AddTool() -- Give Tool Function activates when seat.Occupant changes if seat.Occupant == nil then -- If nobody is in seat when changed if player ~= nil then local hastoolinbackpack = player:FindFirstChild(ToolToGive.Name,true) -- Check players backpack to see if they have it local hastoolonself = player.Character:FindFirstChild(ToolToGive.Name,true) -- Check players actual inventory to see if they have it if hastoolinbackpack ~= nil then -- if so destroy hastoolinbackpack:Destroy() end if hastoolonself ~= nil then -- if so destroy hastoolonself:Destroy() end end player = nil -- reset player variable till next player sits else player = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent) -- Seat occupant get's changed to a characters humanoid, so we can get player by seat.Occupant.Parent if player ~= nil then local hastoolinbackpack = player:FindFirstChild(ToolToGive.Name,true) -- Check players backpack to see if they have it local hastoolonself = player.Character:FindFirstChild(ToolToGive.Name,true) -- Check players actual inventory to see if they have it if hastoolinbackpack == nil and hastoolonself == nil then -- if not give tool local newtool = ToolToGive:Clone() newtool.Parent = player.Backpack end end end end seat:GetPropertyChangedSignal("Occupant"):Connect(AddTool)