Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

2 comments

1

Try just calling random with one value, "math.random(#Colors)" as it automatially assumes you want a range between 1 and #Colors

1

Maybe instead of using Color3, use BrickColor.random()

1 answer

✓ Accepted answer

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)

1 comment

0

THANK YOU just looked at the devforum for dictionaries and arrays

This discussion is preserved as it was. New answers, comments, and votes are closed.