Help with randomized tools

I have Gun tools that I need to be randomized put into each players inventory from somewhere

I dont know how to script so I need theseimage and I need them to be put here!image somehow randomized for each player is there anyway that to do that

I tried asking a friend but i didnt understand so i came here to get a better solution

thank you for reading

edit : if there is a way to kill a player them get another random gun than I would like that if not then its ok

You can achieve this by using math.random.

local Guns = path.to.guns:GetChildren()

local randomGun = Guns[math.random(1,#Guns)]

any detail because i dont know anything about code or where objects go

I’ll try to explain what the code does.

local Guns = path.to.guns:GetChildren()
-- GetChildren() returns an array (a type of table) that contains all the children
-- of a parent

local randomGun = Guns[math.random(1,#Guns)]
-- Since it is an array, the index will be a number (Array is index-value). 
-- We can use the random number to pick what gun.

Currently having trouble explaining it, but I’ll give some links that might explain it better:

Yes I see but i dont know where to put the gun and script in explorer.

You can put the gun to where the server sees, I prefer ServerStorage.

For the script, you can put it in ServerScriptService.

I did that and it didnt go into my inventory

I just did path.to.guns:GetChildren() as an example, you need to check it to game:GetService("ServerStorage").Guns:GetChildren().

its not working still i put it


im not very good at this clearly

local Guns = game:GetService("ServerStorage").Guns:GetChildren()
local randomGun = Guns[math.random(1,#Guns)]

Replace all your code with this.

is that it? because its not working

I think you need to make a folder named “Guns” in ServerStorage and insert the tools inside this folder.

I did that excatly and it still isnt going in my backpack

Currently, the only thing these 2 lines do is select a random tool (Assuming this folder is only filled with tools) in the Guns folder of ServerStorage.

Do you want the selected tool to be randomized every time they spawn to be in their backpack?

yes exactly that you read my mind

In order to do that, you’ll need to connect whenever a character spawns to a function which gives you a random tool of this folder.

Put this in serverscriptservice (this is an addon):

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player) -- Whenever a player is added to server..
	player.CharacterAdded:Connect(function(character) -- Whenever a character is added/respawns..
		local Guns = game:GetService("ServerStorage").Guns:GetChildren()
		local randomGun = Guns[math.random(1,#Guns)]:Clone() -- Clone() for multi-use
		randomGun.Parent = player.Backpack -- It gets sent to the player's backpack
	end)
end)
7 Likes

Thanks Sorry for taking your time