Script help with removing tools when sitting

Hello, so I’ve been trying to make a script that will remove the tools from the player when sitting in a seat(Car seat, or just a normal seat), then when the player gets up it adds the tools back, the script won’t work and I don’t see what’s wrong with it(Script is below) also just note I am new to scripting.

Notes: I have a folder in ServerStroage called “toolsFolder” witch will move all the player’s tools into that folder
Script:

local Driverseat = script.Parent 

local function onPlayerSit(player)
    for _, tool in ipairs(player.Backpack:GetChildren()) do
        if tool:IsA("Tool") then
            tool:Destroy() 
        end
    end
end

local function onPlayerExit(player)
    local toolsFolder = game.ServerStorage:FindFirstChild("Tools") 
    
    if toolsFolder then
        for _, tool in ipairs(toolsFolder:GetChildren()) do
            if tool:IsA("Tool") then
                tool:Clone().Parent = player.Backpack Backpack
            end
        end
    end
end

Driverseat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local occupant = Driverseat.Occupant
    
    if occupant then
        local player = game.Players:GetPlayerFromCharacter(occupant.Parent)
        if player then
            onPlayerSit(player)
        end
    else -- If no occupant (player exited)
        local player = game.Players:GetPlayerFromCharacter(Driverseat.Occupant.Parent)
        if player then
            onPlayerExit(player)
        end
    end
end)

You’re trying to access the parent of the occupant, after you’ve already confirmed that there is no occupant (line 32).

You should instead have a variable that stores the player who sits in the seat, then uses that variable when the player leaves the seat.

e.g.

local Player = nil
Driverseat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local occupant = Driverseat.Occupant
    
    if occupant then
        local player = game.Players:GetPlayerFromCharacter(occupant.Parent)
        if player then
            Player = player
            onPlayerSit(player)
        end
    else -- If no occupant (player exited)
        if Player then 
            onPlayerExit(Player)
        end
    end
end)

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