Get random children

I wanted to make a developer item giver that give random guns and colors from a folder, but it doesn’t work well. It gives me a random and it is the same for the rest of the game. This is my script:

local debounce = false
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local folder = ReplicatedStorage.Weapons.HyperlaserColors:GetChildren()
local tool = folder[math.random(0, #folder)]

– Define function to give tool to the player
local function giveTool(player)
local backpack = player:FindFirstChild(“Backpack”)
tool:Clone().Parent = backpack
print(tool.Name)
–end
end

script.Parent.Button.Touched:Connect(function(hit)
local folder = ReplicatedStorage.Weapons.HyperlaserColors:GetChildren()
local tool = math.random(0, #folder)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
if not debounce then
if player.Name == “ElectricPeaPvZ” then
debounce = true
script.Parent.Button.BrickColor = BrickColor.Red()
giveTool(player)
wait(10)
debounce = false
script.Parent.Button.BrickColor = BrickColor.new(“Lime green”)
end
end
end
return tool
end)

Any help appreciated!

So you’ve tested it say, more than 5 times, and in all 5 times the given tool is exactly the same?

If you want it to choose a random tool every single time shouldn’t this line be inside the Givetool function :

local tool = folder[math.random(0, #folder)]
1 Like

idk if this will change anything but you may need to change

local tool = folder[math.random(0, #folder)]
to
local tool = folder[math.random(1, #folder)]
also make sure you have multiple tools inside the folder

You will need to move line 4 local tool = folder[math.random(0, #folder)] into the giveTool function. When you define a variable, it won’t change unless you specifically made it do so.

Use the RobloxAPI for this.

RobloxAPI:RandomChildren(Children)

1 Like

they’re different for each test. the first random for each one will be the same for the rest of the test and i need to rejoin to get another one.

and since it’s random, it is only a chance.

You can try to make it such that if the Script returns a number that has already been previously called, it’ll “reroll” until it gets a different number. Here’s an example of what you could do

local lastNumber

-- Insert other code here... 
script.Parent.Button.Touched:Connect(function(hit)
  local folder = ReplicatedStorage.Weapons.HyperlaserColors:GetChildren()
  local tool = math.random(0, #folder)
  if not lastNumber or lastNumber == nil then
     lastNumber = tool
  else
     repeat
      tool = math.random(0, #folder)
     until tool ~= lastNumber
     lastNumber = tool
  else

  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  if player and not debounce and player.Name == "ElectricPeaPvZ" then --Remove excessive if statements!!
        debounce = true
        script.Parent.Button.BrickColor = BrickColor.Red()
        giveTool(player)
        wait(10)
        debounce = false
        script.Parent.Button.BrickColor = BrickColor.new(“Lime green”)

 end
return tool
end)

Thanks. It works now, and I got different colors without playing again.

1 Like

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