Script that gives 4 random gears on spawn sometimes gives you 3, 2, 1 or even no gears at all

Beginning scripter here, I am trying to make a PVP game where you spawn with 4 random gears, each one being from a different folder.
I have encountered an issue where after dying a few times, it starts giving you 3 gears on spawn, 2, 1, or none at all. Before, there was 4 scripts for each folder (Weapons, Weapons1, Other, Social), but it had this issue so I tried combining it into this one script but the issue still occurred. The script is below.

wait(0.1)
local serverStorage = game:GetService("ServerStorage") 
local Players = game:GetService("Players")
local Folder = serverStorage:WaitForChild("Weapons"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local randomGear = Folder[math.random(1,#Folder)]
		randomGear.Parent = player.Backpack
		randomGear.Parent = character --this will make tool equip automatically
	end)
end)
local serverStorage = game:GetService("ServerStorage") 
local Players = game:GetService("Players")
local Folder = serverStorage:WaitForChild("Weapons1"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local randomGear = Folder[math.random(1,#Folder)]
		randomGear.Parent = player.Backpack
		randomGear.Parent = character
	end)
end)
local serverStorage = game:GetService("ServerStorage") 
local Players = game:GetService("Players")
local Folder = serverStorage:WaitForChild("Other"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local randomGear = Folder[math.random(1,#Folder)]
		randomGear.Parent = player.Backpack
		randomGear.Parent = character
	end)
end)
local serverStorage = game:GetService("ServerStorage") 
local Players = game:GetService("Players")
local Folder = serverStorage:WaitForChild("Social"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local randomGear = Folder[math.random(1,#Folder)]
		randomGear.Parent = player.Backpack
		randomGear.Parent = character
	end)
end)

Is there any way I can optimize or change this script into giving you 4 gears every time you spawn without issue?

local serverStorage = game:GetService("ServerStorage") 
local Players = game:GetService("Players")
local Folder = serverStorage:WaitForChild("Other"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
for i = 1,4 do -- a loop that runs 4 times
		local randomGear = Folder[math.random(1,#Folder)]
		randomGear.Parent = player.Backpack
		randomGear.Parent = character
end--end the loop
	end)
end)

This only gives you gears from the “Other” folder – there’s 4 folders the gears are each given from: Weapons, Weapons1, Other, Social. This also still has the issue where it gives you 3, 2 or no gears at all after dying a bunch. Thank you, though!

I think the issue is you are using the same variable to represent each folder. Try changing the name of each variable

local Folder1 = serverStorage:WaitForChild("Weapons"):GetChildren()
local Folder2 = serverStorage:WaitForChild("Other"):GetChildren()

so on

try this:

local serverStorage = game:GetService("ServerStorage") 
local Players = game:GetService("Players")

local Folder1 = serverStorage:WaitForChild("Weapons"):GetChildren()
local Folder2 = serverStorage:WaitForChild("Weapons1"):GetChildren()
local Folder3 = serverStorage:WaitForChild("Other"):GetChildren()
local Folder4 = serverStorage:WaitForChild("Social"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local randomGear1 = Folder1[math.random(1,#Folder1)]:Clone()
		local randomGear2 = Folder2[math.random(1,#Folder2)]:Clone()
		local randomGear3 = Folder3[math.random(1,#Folder3)]:Clone()
		local randomGear4 = Folder4[math.random(1,#Folder4)]:Clone()
		humanoid:EquipTool(randomGear1)
		humanoid:EquipTool(randomGear2)
		humanoid:EquipTool(randomGear3)
		humanoid:EquipTool(randomGear4)
	end)
end)

I made the gears clone out of the folder, which im not sure if intended, but otherwise eventually you’d run out of tools to equip because they’d all be gone from the folder, but here since we clone them the original tools will always be in this folder.

This doesn’t give any gears when you spawn, much thanks though

I might’ve made a mistake with the equiptool thing, hold on.
It would help if you sent a screenshot of ServerStorage.

sstorage

tested this and it worked

local serverStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local Folder1 = serverStorage:WaitForChild("Weapons"):GetChildren()
local Folder2 = serverStorage:WaitForChild("Weapons1"):GetChildren()
local Folder3 = serverStorage:WaitForChild("Other"):GetChildren()
local Folder4 = serverStorage:WaitForChild("Social"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local randomGear1 = Folder1[math.random(1, #Folder1)]
		local randomGear2 = Folder2[math.random(1, #Folder2)]
		local randomGear3 = Folder3[math.random(1, #Folder3)]
		local randomGear4 = Folder4[math.random(1, #Folder4)]
		humanoid:EquipTool(randomGear1)
		randomGear2.Parent = player.Backpack
		randomGear3.Parent = player.Backpack
		randomGear4.Parent = player.Backpack
	end)
end)

2 Likes