Pick random part with varying end to part

Hello, Im trying to make a script randomly pick a part from within a folder of parts. The parts name begins the same however the number at the end varies. I need it to pick the correct part and then randomly pick one of these numbers. If that makes sense…

For example, The parts in the folder are Gun1, Gun2, Gun3 etc among other parts.

I need to be able to ensure the part picked is called Gun and then randomly pick one of these guns?

Hopefully that makes sense

Thankyou
Tom

local Folder = YourFolder
local Children = YourFolder:GetChildren()

local function GetRandom()
local Num = math.random(1, #Children)
local Item = Num:Clone(); Item.Parent = YourLocation

end

1 Like

Thankyou for your quick response, How would this be implemented?

As for my example above I don’t understand how this would check if one of the children are called Gun and if there are multiple results how it would then randomly pick one of them?

Thankyou
Tom

To put it simply, GetChildren gets all Children within your Folder and Turns it into a Table,
The math.random gets a Random Item within the Table,
Since the Items inside the Table are Model, Tools, etc, They will be able to be cloned

Example:

Numbers = { -- Misleading Table
[1] = "Hi", -- First Item in the Table
[2] = "Hello",
[3] = "Hey"
}
print(Numbers[1]) -- Prints the First Item in the Table
print(#Numbers) -- Prints the Number of Items in the Table
print(Numbers[math.random(1, #Numbers)]) -- Prints a Random Item in the Table

Use a Server Script and place it somewhere like ServerScriptService

Thankyou for your reply, Its not really doing what I need it to do to be honest. I thought of the method of tweaking what you have for example,

local Children = game.workspace.GunsFolder:GetChildren()

local function GetRandom()
local RandomItem = math.random(1, #Children)
If RandomItem.Name == “Gun”…[] then
Print"GunFound"
End

But how would I just ignore the fact the Gun is numbered so it just finds the string in the parts name called gun?

end

1 Like