Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

HOW TO REMOVE TOOL FROM PLAYER?

Asked by 4 years ago

hi thank you for your time and help i really appreciate it i have a simple problem i want to remove all tools from all players and then give it back to them when i want here is my code for doing it

local items=game.StarterPack:GetChildren()
for ii,item in pairs(items) do
    item.Parent=ServerStorage:WaitForChild("itemholder") -- put's it into a model
end

but t didn't work thank you if u have any idea please answer it.

1 answer

Log in to vote
1
Answered by
oilkas 364 Moderation Voter
4 years ago

The StarterPack is not the place where the Player's tools are stored, they move to the Player's Backpack after the game runs. To remove tools from all players, you can do this.

for _,player in pairs(game.Players:GetChildren()) do
    for _,tool in pairs(player.Backpack:GetChildren()) do
        tool.Parent = ServerStorage:FindFirstChild("itemholder") -- This way, the tools get mixed up with other players' tools.
    end
end

This way, the tools get mixed up and you won't be able to give them back later, to fix this, you can add this in a server script:

local holder = ServerStorage:WaitForChild("itemholder")

game.Players.PlayerAdded:Connect(function(player)
    local folder = Instance.New("Folder")
    folder.Name = player.Name
    folder.Parent = holder
end)

game.Players.PlayerRemoving:Connect(function(player)
    local folder = holder:FindFirstChild(player.Name)
    if folder then
        folder:Destroy()
    end
end)

So, Your final script to remove all the tools from the players would be

for _,player in pairs(game.Players:GetChildren()) do
    for _,tool in pairs(player.Backpack:GetChildren()) do
        tool.Parent = ServerStorage:FindFirstChild("itemholder"):FindFirstChild(player.Name) -- This way, the tools wont get mixed up with other players' tools.
    end
end
0
thank u really but this line of code in the server script game.Players.PlayerAdded:Connect(function(player) local folder = Instance.New("Folder") folder.Name = player.Name folder.Parent = holder end) gives me serverScriptService.main:67: attempt to call a nil value you_success -50 — 4y
0
does itemholder exist in the serverstorage? oilkas 364 — 4y
0
yes it does you_success -50 — 4y
0
but should i put the for _,player in pairs(game.Players:GetChildren()) do 2 for _,tool in pairs(player.Backpack:GetChildren()) do 3 tool.Parent = ServerStorage:FindFirstChild("itemholder"):FindFirstChild(player.Name) in local or server because i have put it into a server script you_success -50 — 4y
View all comments (9 more)
0
and when i play my game i can not open the server storage folder in explore is it a normal thing or does it mean that threre is nothing in it you_success -50 — 4y
0
try using replicatedstorage instead of serverstorage oilkas 364 — 4y
0
ok thanks you_success -50 — 4y
0
thank you oilkas it worked really thank you you_success -50 — 4y
0
the tolls dissapear but it still shows the error is it a big deal you_success -50 — 4y
0
what is the error? oilkas 364 — 4y
0
the same error erverScriptService.main:67: attempt to call a nil value thiis line local folder = Instance.New("Folder") you_success -50 — 4y
0
thats odd oilkas 364 — 4y
0
maybe i should create the itemholder in my script you_success -50 — 4y
Ad

Answer this question