Ad
Log in to vote
0

Why does this script not give me a tool when I sit on a seat?

Asked by 3 months ago

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.

1 answer

Log in to vote
0
Answered by 3 months ago
Edited 3 months ago

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) 
0
Works! Thank you so much! Thanks for taking time out of your day for helping me! It did work. Will know this the next time, thanks again! :D SonGohan6 8 — 3mo
0
No problem! Happy coding :) WizyTheNinja 834 — 3mo
Ad

Answer this question