Random functions are not working

The script that i have have two random functions: Random numbers for the player fake id and a random color for it’s torso which is indexed:


local Colors = {
	["Red"] = 255, 0, 0;
	["Orange"] = 255, 127, 0;
	["Yellow"] = 255, 255, 0;
	["Green"] = 0, 255, 0;
	["Blue"] = 0, 0, 255;
	["Green"] = 255, 0, 127;
}

local BannedNumbers = {
	616;
	666;
}

--// Index //--

local Torso = script.Parent:FindFirstChild("Torso")
local BotNumber = Torso:FindFirstChild("BotNumber"):FindFirstChild("BotNumber")

--// Functions //--

--Random color:
local RandomColor = (Colors[math.random(1, #Colors)])
Torso.Color3 = Color3.new(RandomColor)
print(RandomColor)

--Random number:
local RandomNumber
	repeat RandomNumber = math.random(100,999) print(RandomNumber) --repeat until the selected random number is not banned.
	until RandomNumber ~= #BannedNumbers
BotNumber.Text = RandomNumber

By any reason, those are not working, and if i can see, they are not printing any error, which is strange for me. Thanks for reading :+1:

2 Likes

You should add wait() in the repeat and until, a cooldown, because your game might crash.

Are you trying to make it so the torso changes colour? I don’t really understand what you are trying to do.

@ignacasas06 Something like this?:

repeat RandomNumber = math.random(100,999) print(RandomNumber) wait()
until RandomNumber ~= #BannedNumbers

@Inkthirsty Yes:

Is BodyColors interfering with your script?

No, i have deleted it.
30chars

#BannedNumbers is evaluating as ‘2’ because # just returns the length of an array.
Try:

until table.find(BannedNumbers, RandomNumber) == nil

Oh I think I know what’s wrong. You did Torso.Color3, it should be Torso.Color. Try this:

local Colors = {
[“Red”] = 255, 0, 0;
[“Orange”] = 255, 127, 0;
[“Yellow”] = 255, 255, 0;
[“Green”] = 0, 255, 0;
[“Blue”] = 0, 0, 255;
[“Green”] = 255, 0, 127;
}

local BannedNumbers = {
616;
666;
}

–// Index //–

local Torso = script.Parent:FindFirstChild(“Torso”)
local BotNumber = Torso:FindFirstChild(“BotNumber”):FindFirstChild(“BotNumber”)

–// Functions //–

–Random color:
local RandomColor = (Colors[math.random(1, #Colors)])
Torso.Color = Color3.new(RandomColor)
print(RandomColor)

–Random number:
local RandomNumber
repeat RandomNumber = math.random(100,999) print(RandomNumber) --repeat until the selected random number is not banned.
until RandomNumber ~= #BannedNumbers
BotNumber.Text = RandomNumber

How you’re declaring the colors table, you’re giving Red the value of 255 - the zeros get added as seperate entries to the table I would just store the color3 value instead of the rgb colours


local Colors = {
	["Red"] = Color3.fromRGB(255, 0, 00;
        --and so on..
}

Your RandomNumber is a bit off too, as you’re checking that RandomNumber ~= #BannedNumbers, which is the amount of entries in the table which is 2 i.e.

local BannedNumbers = {
	616;
	666;
}
 RandomNumber ~= #BannedNumbers <==>  RandomNumber ~= 2

This should do the trick:

local BannedNumbers = {
	616;
	666;
}

local function tableContains(TABLE, VALUE)
    for _,ENTRY in pairs(TABLE) do
      if ENTRY == VALUE then return true end
    end

    return false;
end

local RandomNumber
	repeat RandomNumber = math.random(100,999) print(RandomNumber) --repeat until the selected random number is not banned.
	until tableContains(BannedNumbers, RandomNumber) == false;

This isn’t doing what you think.

local Colors = {
	["Red"] = 255, 0, 0;
	["Orange"] = 255, 127, 0;
	["Yellow"] = 255, 255, 0;
	["Green"] = 0, 255, 0;
	["Blue"] = 0, 0, 255;
	["Green"] = 255, 0, 127;
}

It’s a mixed table. You’re basically writing this but weirdly:

local Colors = {
	Red = 255,
	0,
	0,
	Orange = 255,
	127,
	...
}

You’d want to have an array of colors to pick one at random.

local Colors = {
 Color3.fromRGB(255,0,0);
 Color3.fromRGB(255,127,0);
 Color3.fromRGB(255,255,0);
 Color3.fromRGB(0,255,0);
 Color3.fromRGB(0,0,255);
 Color3.fromRGB(255,0,127);
}

Torso.Color = Colors[math.random(#Colors)]
3 Likes

Allow me to change your life forever:
Introducing…

table.find()!

3 Likes

when you use #BannedNumbers it’s thinking that its 2 because you put 2 values in the table and # is for evaluting the lenght of the table

1 Like

@Blokav @boatbomber @JoelBrd @Inkthirsty

It’s not working, well, i should give the model i guess