The tools are moving into the player’s backpacks but it doesn’t show up in the toolbar of the player. I found a fix for this, it’s that you have to use WaitForChild:("Backpack"). That normally works, but this is about more player’s backpacks at the same time. What did I do wrong?
function GiveTools()
for i,v in ipairs(game.Players:GetChildren()) do
if v.Team == game.Teams["Alive"] then
local tool = tools[math.random(1, #tools)]
local toolClone = tool:Clone()
local backpack = v:WaitForChild("Backpack")
toolClone.Parent = backpack
else end
end
end
Does this code runs again when the player dies?
By the way some changes to the code:
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
function GiveTools()
for i, v in pairs(Players:GetChildren()) do
if v.Team == Teams["Alive"] then
local tool = tools[math.random(#tools)]
local toolClone = tool:Clone()
local backpack = v:FindFirstChild("Backpack") or v:WaitForChild("Backpack", 0.1)
toolClone.Parent = backpack
end
end
end
@focasds
It didn’t work. After the function, the tools will be in the player’s inventories but not in their tool bar. It didn’t run again when the player dies. It only works when a certain gamemode is chosen at the beginning. If the other gamemode is chosen, and after that the tools gamemode, then it doesn’t show up in the tool bar.
local gamemodes = {
"other gamemode";
"tools"
}
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
function GiveTools()
for i, v in pairs(Players:GetChildren()) do
if v.Team == Teams["Alive"] then
local tool = tools[math.random(#tools)]
local toolClone = tool:Clone()
local backpack = v:FindFirstChild("Backpack") or v:WaitForChild("Backpack", 0.1)
toolClone.Parent = backpack
end
end
end
while true do
-- intermission
if gamemode == "other gamemode" then
-- other gamemode
elseif gamemode == "tools" then
wait(5)
GiveTools()
end
end
By the way, you shouldn’t do object:FindFirstChild() or object:WaitForChild().
I used to do the same mistake but the WaitForChild function looks something like this:
local function WaitForChild(inst, name)
local object
repeat object = inst:FindFirstChild(name) wait() until object
return object
end
(of course not exactly, but you get what I mean), it’s basically using FindFirstChild until the instance is found, so you can just remove the FindFirstChild.