How would I make a script that gives a player a random tool on spawn?

How would I make a script that whenever the player spawns, the game gives the player a random tool from let’s say replicated storage?

1 Like

This should work:

local RandomTool = game.ReplicatedStorage.Tools:GetChildren()[math.random(1, #game.ReplicatedStorage.Tools:GetChildren())] -- we get a random tool from the tools folder in the ReplicatedStorage
RandomTool.Parent = Player.Backpack -- we set the parent of the tool to the player

If you want to use it multiple times then you can clone the “picked” tool.

2 Likes

Assuming that you have your have your tools in a folder named “Tools” and you have 5 tools, then this should be your code:
local RandomTool = 0
local ToolOne = game.ReplectedStorage.Tools.ToolOne:Clone()
local ToolTwo = game.ReplectedStorage.Tools.ToolTwo:Clone()
local ToolThree = game.ReplectedStorage.Tools.ToolThree:Clone()
local ToolFour = game.ReplectedStorage.Tools.ToolFour:Clone()
local ToolFive = game.ReplectedStorage.Tools.ToolFive:Clone()
game.Players.LocalPlayer.CharacterAdded:Connect(function()
RandomTool = math.Random(1, 5)
if RandomTool == 1 then
ToolOne.Parent = game.Players.LocalPlayer.Backpack
end
if RandomTool == 2 then
ToolTwo.Parent = game.Players.LocalPlayer.Backpack
end
if RandomTool == 3 then
ToolThree.Parent = game.Players.LocalPlayer.Backpack
end
if RandomTool == 4 then
ToolFour.Parent = game.Players.LocalPlayer.Backpack
end
if RandomTool == 5 then
ToolFive.Parent = game.Players.LocalPlayer.Backpack
end
end)
This should work. If it doesn’t, then I don’t know.

1 Like

This answer is not a very good solution, because there’s a lot of if statements, I suggest @DEVLocalPlayer’s answer

1 Like