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

Can somebody explain this simple yet bizarre code interaction to me?

Asked by
TomsGames 225 Moderation Voter
4 years ago
Edited 4 years ago

Hi ScriptingHelpers!

Messing around with some ROBLOX programming and I've ran into a really strange interaction I can't figure out.

This is my code:

for i, v in pairs (game.Workspace.PlayBase.Plates:GetChildren()) do
    local snum = string.sub(v.Name, 6)

    local rnum = math.random(8500, 9500) * snum
    local cl = Color3.fromRGB(rnum, rnum, rnum)

    local cl2 = Color3.fromRGB(math.random(8500, 9500) * snum, math.random(8500, 9500) * snum, math.random(8500, 9500) * snum)

    v.Color = cl2 -- OR cl.
end

PlayBase.Plates:GetChildren() are 25 plates which are set up in a 5x5 grid.This code is trying to set a random pattern of colours. The code works, but there's a catch.

When I choose to set v.Color to "cl2" the brick colour generates in the way that I would expect, as seen from this gyazo.

However when I set the v.Color to just "cl", which in my head is generated in EXACTLY the same way as cl2, you can see it does not act in the same way.

In my head cl and cl2 are generated in the same way, with a new Color3.fromRGB value, with the R, G, and B values being generated by "math.random(8500, 9500) * snum". Why are the outcomes consistently different? Am I missing something?

0
What number does snum equal? RGB values only go up to 255 so snum must be a very low number? ForeverBrown 356 — 4y

1 answer

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

You're only picking a random number once with c1. If you print out c1 there times you'll see they're all the same number.

Three of the same number make a grey color with rgb.

Instead create three variables or directly put the math.randoms in the parameters like you did with c12

1
'Three of the same number make a grey color with rgb' - Ah, now I feel dumb TomsGames 225 — 4y
1
you're not dummy, it can be confusing. In programming you'll always have mistakes, but you get better at recognizing mistakes as you go royaltoe 5144 — 4y
Ad

Answer this question