How can I make random gear script give 2 gears instead of just 1?

Fairly new scripter here,
I have a script here that gives you a random gear on spawn. I’m not sure how to make it give you multiple gears and there’s not really any tutorials on this. How can you modify the script below to make it grant you 2 gears?


local HttpService = game:GetService("HttpService")
local InsertService = game:GetService("InsertService")

local worldInfo = HttpService:JSONDecode(script.Parent.WorldInfo.Value)

function giveGear(plr, gearId, gearGiverId)
	if gearGiverId then
		if findGear(plr, gearGiverId) then
			return
		end
	end
	if gearId then
		local model = InsertService:LoadAsset(gearId)
		
		local tool = model:FindFirstChildOfClass("Tool")
		tool.CanBeDropped = (worldInfo.AllowDropping and tool.CanBeDropped) or worldInfo.AllowDropping
		tool.Parent = plr.Backpack
		
		if gearGiverId then
			local gearGiverIdValue = Instance.new("StringValue")
			gearGiverIdValue.Name = "GearGiverId"
			gearGiverIdValue.Value = gearGiverId
			gearGiverIdValue.Parent = tool
		end
		
		model:Destroy()
	end
end
script.GiveGear.OnInvoke = giveGear

function findGear(plr, gearGiverId)
	for _, gear in pairs({plr.Character:FindFirstChildOfClass("Tool"), unpack(plr.Backpack:GetChildren())}) do
		if gear:FindFirstChild("GearGiverId") and gear.GearGiverId.Value == gearGiverId then
			return gear
		end
	end
end
script.FindGear.OnInvoke = findGear

function giveGears(plr, gearsList)
	for _, gearId in pairs(gearsList) do
		giveGear(plr, gearId)
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if worldInfo.Gears then
			giveGears(plr, worldInfo.Gears)
		end
		if plr.Team then
			if worldInfo.TeamGears then
				local teamGears = worldInfo.TeamGears[plr.Team.Name]
				if teamGears then
					giveGears(plr, teamGears)
				end
			end
		end
	end)
end)

Change this

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if worldInfo.Gears then
			giveGears(plr, worldInfo.Gears)
		end
		if plr.Team then
			if worldInfo.TeamGears then
				local teamGears = worldInfo.TeamGears[plr.Team.Name]
				if teamGears then
					giveGears(plr, teamGears)
				end
			end
		end
	end)
end)

To this

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if worldInfo.Gears then
			for i = 1 , 2 do --change 2 to how many gears you want to give
				giveGears(plr, worldInfo.Gears)
			end
		end
		if plr.Team then
			if worldInfo.TeamGears then
				local teamGears = worldInfo.TeamGears[plr.Team.Name]
				if teamGears then
					for i = 1 , 2 do --change 2 to how many gears you want to give
						giveGears(plr, worldInfo.Gears)
					end
				end
			end
		end
	end)
end)

my bad, mistake

local gearsFolder = path

local RandomGears = gearsFolder[math.random(2, #gearsFolder)]

game.Players.PlayerAdded:Connect(function(player)
     for i,v in pairs(gearsFolder:GetChildren()) do
        if v:IsA("Tool") then
            local RandomGears = v[math.random(2, #v)]
            v:Clone().Parent = player.Backpack
       end
   end
end)

Doesn’t seem to work, thank you though

Did it send an error??? Did it even give you any gear?

It doesn’t give any gear. In dev console when you spawn it says "WorldInfo is not a valid member of ServerScriptService “ServerScriptService”

1 Like

Change

local worldInfo = HttpService:JSONDecode(script.Parent.WorldInfo.Value)

To

local worldInfo = HttpService:JSONDecode(script.Parent:WaitForChild("WorldInfo").Value)

Odd, still says the same exact thing about WorldInfo not being a valid member of ServerScriptService.

1 Like