Give each player a random tool from a folder/table

So i’m making a game that gives each player a random tool from a folder (or table).
Well i don’t exactly know how to do that.
I’ve tried doing this, but it doesn’t work :

local RemoteEvent = game.ReplicatedStorage.GiveTools
local player = game.Players.LocalPlayer
local Tools = {
	Katana = {game.ReplicatedStorage.DefaultTools.Katana};
	Machete = {game.ReplicatedStorage.DefaultTools.Machete};
	Sword = {game.ReplicatedStorage.DefaultTools.Sword}
}


local MainTitle = player.PlayerGui.Intermission.Title

RemoteEvent.OnClientEvent:Connect(function()
	for i=5, 0, -1 do
		wait(1)
		MainTitle.Text = "Game starts in "..i.."s"
	end
	
	for _, plr in pairs(game:GetService("Players"):GetChildren()) do
		if plr:IsA("Player") then
			plr.Character.HumanoidRootPart.CFrame = workspace.TeleportPart.CFrame
			local PossibleTools = Tools:GetChildren()
			local choosenTool = PossibleTools[math.random(1, #PossibleTools)]	--Here is my attempt on selecting a random tool.

			print(choosenTool)
		end
	end
end)

Any help will be appreciated!

Hey there,

so I’ll start with a simple example to maybe give you a general idea/help

local items_folder = game.ServerStorage.Items -- you can change this path to the folder where u store the items
local players = game:GetService("Players")

while true do
	task.wait(3)

	for _,player in pairs(players:GetPlayers()) do
		local random_item = items_folder:GetChildren()[math.random(1,#items_folder:GetChildren())]:Clone()
		--These 2 are optional, using it to prevent player from getting duplicates, you can remove it if you dont want it
		if player.Backpack:FindFirstChild(random_item.Name) then continue end
		if player.Character:FindFirstChild(random_item.Name) then continue end
		random_item.Parent = player.Backpack
	end
end

Now to your case,
try this -

local RemoteEvent = game.ReplicatedStorage.GiveTools
local player = game.Players.LocalPlayer
local Tools = {
	game.ReplicatedStorage.DefaultTools.Katana,
	game.ReplicatedStorage.DefaultTools.Machete,
	game.ReplicatedStorage.DefaultTools.Sword
}


local MainTitle = player.PlayerGui.Intermission.Title

RemoteEvent.OnClientEvent:Connect(function()
	for i=5, 0, -1 do
		wait(1)
		MainTitle.Text = "Game starts in "..i.."s"
	end

	for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
		if plr:IsA("Player") then
			plr.Character.HumanoidRootPart.CFrame = workspace.TeleportPart.CFrame
			local PossibleTools = Tools
			local choosenTool = PossibleTools[math.random(1, #PossibleTools)]	--Here is my attempt on selecting a random tool.

			print(choosenTool)
		end
	end
end)

When you use a table like this :

local myTab = {"a","b","c"}

you dont need to use GetChildren() when calling it

2 Likes

Oh god, thank you so much for helping me out!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.