Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Attempting to get random from a table returns an error??

Asked by 3 years ago

Hello...I'm trying to select a random color value from a table but it returns with Workspace.Scripts.randomNPC:14: invalid argument #2 to 'random' (interval is empty)

Here is a snippet of my script:

local Colors = {
    Color1 = Color3.fromRGB(255,229,180),
    Color2 = Color3.fromRGB(222,184,135),
    Color3 = Color3.fromRGB(139,69,19)
}
local randomColor = Colors[math.random(1,#Colors)]
for _,limb in pairs(npc:GetChildren()) do
    if limb:IsA("BasePart") then limb.Color = randomColor end
end

help?

1
Try just calling random with one value, "math.random(#Colors)" as it automatially assumes you want a range between 1 and #Colors Benbebop 1049 — 3y
1
Maybe instead of using Color3, use BrickColor.random() Finty_james 269 — 3y

1 answer

Log in to vote
1
Answered by
k3du53 162
3 years ago

Try making the index for each value of Colors an integer 1, 2, or 3 instead of Color1, etc.

The reason for the error is that #Colors equals 0, and (1,0) is an empty interval.

(You have a dictionary instead of an array)

0
THANK YOU just looked at the devforum for dictionaries and arrays Omq_ItzJasmin 666 — 3y
Ad

Answer this question