#How would I clear the backpack on death and give the player a tool based on the team

1 messages · Page 1 of 1 (latest)

wary ivy
#

?

#
local ServerStorage = game:GetService("ServerStorage")

local TOOL_NAME = "Shield" -- Replace with your tool's name

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        -- Wait for the Backpack to exist
        local backpack = player:WaitForChild("Backpack")

        local tool = ServerStorage:FindFirstChild(TOOL_NAME)
        if tool then
            local clonedTool = tool:Clone()
            clonedTool.Parent = backpack
        else
            warn("Tool not found in ServerStorage:", TOOL_NAME)
        end
    end)
end)
```lua
vestal charm
wary ivy
#

yeah this was generated by chatgpt

#

wasnt trying to claim it or anything

#

sorry if it seemed that way

light tangle
#

how did u get s1 tho

wary ivy
#

this is like

#

the only time ive used gpt

#

im newer but i did make a game

light tangle
#

ok so

#

how would u without gpt

#

clear backpack

wary ivy
#

Player:WaitForChild("Backpack"):ClearAllChildren()

#

i think

#

but im more focused on the team specific gear

light tangle
#

just

#

detect team

#

and give tool

wary ivy
#

i have that

#

but whenever i switch teams it doesnt give me the new tool

light tangle
#

i dont think u even got team's code

#

on what u sent

#

and u got one tool so idk what u expect

#

and

#

does changing team kill u?

wary ivy
#

yeah i set that up in a different script

#

but like

#

i had tried making an if statement

#

that changes TOOL_NAME based on what team you were on

#

but it didnt work

light tangle
#

show.

wary ivy
#
local ServerStorage = game:GetService("ServerStorage")
 local TOOL_NAME = nil

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        
        if player.Team == game.Teams["Thunderer"] then
            local TOOL_NAME = "Hammer"
        elseif player.Team == game.Teams["Super Soldier"] then
            local TOOL_NAME = "Shield"
        end
        
        -- Wait for the Backpack to exist
        local backpack = player:WaitForChild("Backpack")

        local tool = ServerStorage:FindFirstChild(TOOL_NAME)
        if tool then
            local clonedTool = tool:Clone()
            clonedTool.Parent = backpack
        else
            warn("Tool not found in ServerStorage:", TOOL_NAME)
        end
    end)
end)
#

i just put the backpack clearing in another script that handles the team changing

light tangle
#

and what do u get in this script?

#

any errors

#

or what happends

wary ivy
#

13:08:59.565 Argument 1 missing or nil - Server - ToolEquipper:17

#

just this

light tangle
#

local tool = serverstorage?

wary ivy
#

yeah

light tangle
#

on why

#

TOOL_NAME

#

can be nil

wary ivy
#

i mean

#

i declared it at the top

#

oh

#

do i need to return it in the if statement

vestal charm
#

the inner local TOOL_NAME declarations shadow the outer variable, TOOL_NAME remains nil when you call FindFirstChild(nil), triggering the “Argument 1 missing or nil” error

wary ivy
#

oh

#

how would i fix that

#

remove local?

light tangle
#

u can remove the line 3 local tool

#

cuz idfk what u need it for

#

and

#

the error is cuz toolname is always nil

#

in your case, neither of the team checks was sucess

#

i believe

wary ivy
#
local ServerStorage = game:GetService("ServerStorage")


Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        
        local TOOL_NAME
        if player.Team == game.Teams["Thunderer"] then
            TOOL_NAME = "Hammer"
        elseif player.Team == game.Teams["Super Soldier"] then
            TOOL_NAME = "Shield"
        end
        
        print(TOOL_NAME)
        -- Wait for the Backpack to exist
        local backpack = player:WaitForChild("Backpack")

        local tool = ServerStorage:FindFirstChild(TOOL_NAME)
        if tool then
            local clonedTool = tool:Clone()
            clonedTool.Parent = backpack
        else
            warn("Tool not found in ServerStorage:", TOOL_NAME)
        end
    end)
end)
#

I fixed tool name from being nil

#

but for some reason it doesnt recognize when i switch teams

light tangle
#

record rq

wary ivy
vestal charm
#

your script only ever runs the tool giving logic inside the initial character added handler, so if you switch teams and respawn at the same time then the comparison against player.Team still sees the old or nil value and leaves TOOL_NAME unset 💔

wary ivy
#

i thought chartacter added runs each time the character respawns

torn shuttleBOT
#

studio** You are now Level 4! **studio

vestal charm
#

listen for the player’s team change (player:GetPropertyChangedSignal("Team")) and rerun your tool-assignment code after a team switch

wary ivy
#

one last thing