So I'm making a game and most of it is based off of random placement, I made a simple script that used math.random with numbers 1 - 3, and whichever number it said determined the output
But then I realized I don't want the 3 outputs to have the same frequency, because I want one to be more common than the other.
I'm not sure how to make a script to accomplish this, so I was thinking having a range of numbers 1 - 10, and if it's 1 - 6 it's one, 7 - 8 another, and 9 - 10 the other.
I think there is another way to do this, but I'm not sure.
This is the current script that has an error saying:
Workspace.Script:7: attempt to compare boolean with number
while true do wait(1) local type = math.random(1,10) if type <7 then print("grass") elseif type >6 <9 then print("road") elseif type >8 <11 then print("water") end end
First, you should indent your code correctly. It makes it much easier to read, and therefore much easier to catch mistakes. Likewise, you should generally use spaces whenever possible to split things up visually, such as around operators like (<) and (>).
while true do wait(1) local type = math.random(1,10) if type < 7 then print("grass") elseif type > 6 < 9 then print("road") elseif type > 8 < 11 then print("water") end end
Although it isn't related to the error you're having, you should avoid defining a variable named type
. That's because type
is a function that's built-in to Lua which tells you the type of a parameter (type(4)
is "number"
and type("hello!")
is "string"
).
The main source of your error, though, it exactly where it's telling you. On line 7, you're attempting to compare a number with a boolean.
Why?
We have this expression to evaluate: type > 6 < 9
. Okay, let's pretend that we say type = 7
just before this. How does Lua understand this?
type = 7 -- define type to be 7
So we want to evaluate this:
type > 6 < 9
Let's plug in type
's value:
7 > 6 < 9
As Lua understand this code, there are parentheses there. Even though you didn't write them, they're implied by the parsing rules. I'll show them to you:
(7 > 6) < 9
Okay, great. So let's evaluate what's inside the parentheses.
Since 7 > 6 is obviously true, we get (true)
.
(true) < 9
And now drop those pointless parentheses:
true < 9
But what is this supposed to mean? I don't know whether true
is more or less than 9
. Lua doesn't either, that's why it's crashing.
What you actually want to say is:
type > 6 and type < 9
The and
operator (it's not a variable) takes something at its left and its right. If they're both true, then the result is true. Otherwise it's false. (Technically it is more complicated than this. Until you understand the above, however, ignore this bit: The left argument is evaluated; if it is falsey, it is returned, otherwise the right argument is evaluated and returned).
So we can fix your code by writing:
while true do wait(1) local type = math.random(1,10) if type < 7 then print("grass") elseif type > 6 and type < 9 then print("road") elseif type > 8 and type < 11 then print("water") end end
On line 7 you tried to do an
and statement, without putting the and. We can fix this easily by chanign line 7 to:
elseif type > 6 and type < 9 then
Easy, common mistake. Now let's look at your question and find a solution.
I have yet to write a good answer on tables, so I figure I can do that while still answering your question.
Tables are extremely great tools, mainly in situations like this. Let's set up 3 tables to use, which would mean 3 classification levels. Our levels will be;
Common
Moderate
Extremely Rare
Let's setup the tables:
common = {"Common stuff here.."} Moderate = {"Moderate 1", "Moderate 2", "etc..."} Rare = {"asdasd","asdasd"}
You are on the right track with using math.random, but to make it even more rare, we are going to use math.random, and make our maximum random up to 100.
Let's try it out:
path = game.Lighting["Stuff"] -- The model/file/place in which all the things you want to spawn are in. toSpawn = game.Workspace -- Where the thing that's cloned would go into. function spawn() local random = math.random(1, 100) if random <= 50 then local RandomInTable = math.random(1, #common) local part = path:FindFirstChild(common[RandomInTable]) local partClone = part:Clone() partClone.Parent = toSpawn elseif random > 50 and random <= 80 then local RandomInTable = math.random(1, #common) local part = path:FindFirstChild(Moderate[RandomInTable]) local partClone = part:Clone() partClone.Parent = toSpawn else -- Now we can just use else, no other situations are possible. local RandomInTable = math.random(1, #common) local part = path:FindFirstChild(Rare[RandomInTable]) local partClone = part:Clone() partClone.Parent = toSpawn end
Now, with that, you would just need to call the function spawn.
Hopefully I helped you even a little bit. Even if I just pushed you into your own thought. Good luck to your development, and feel free to leave any questions in the comments about my explanation.