###Difference between else if and elseif
To more clearly see what is going on when you type 'else if', you should separate it onto separate lines (yes this can, and for clarity probably should, be represented on multiple lines):
This:
else if condition then [CODE] end
end
Is the same as:
else
if condition then
[CODE]
end
end
In the end 'elseif' and 'else if' should have no logical difference (just cascading vs nested if-else statements), however in Lua it is easy to stumble on putting the correct amount of 'end's if you use 'else if' (so i suggest using 'elseif').
###Random and Randomseed
The random function works by determining a random number using the previously found random number, therefore it must start at a certain number. This number is called the 'seed'. What you will find is that if you give the pseudo-random number generator the same seed, it will generate the same sequence of numbers.
This can be very helpful if you want to re-simulate an event, but not so nice if you want something different to happen each time you start the game, for instance. The way around this is to set the seed, using the randomseed function, to something different each time you start the game. A common way to do this is using the following line:
math.randomseed(tick())
Tables Help
As for tables, your question was not clear. Check out this wiki article on tables, hopefully it will clear things up.
Edit 1
The 'tick' function does deal with time, which is why we use it as a seed. Time is always increasing, therefore it will be a different number each time we use it as a seed. So when we write math.randomseed(tick()), the seed will be different every time.
As for your problem, choosing a random crate would be quite easy. You just have to choose a random number between 1 and the number of crates and get the crate and that index in the table.
local crates = {"Common", "Uncommon", "Rare", "Ultrarare", "Epic", "Legendary"}
math.randomseed(tick())
script.Parent.MouseButton1Click:connect(function()
local crateName = crates[math.random(#crates)]
local crate = game.ServerStorage.Crates[crateName]:Clone()
end)
For your particular case, I do not think you want completely random crate drops. That would mean you have the same chance of getting Legendary crates as you do Common ones. One way to fix this is to give each crate a probability of being selected. In my case, I made all the probabilities add up to 100 (think percentages), so I'll generate a random number between 0 and 100 (math.random()*100). Then I simply compare to see which crate should be chosen:
local crates = {"Common", "Uncommon", "Rare", "Ultrarare", "Epic", "Legendary"}
local probability = {50, 25, 10, 7.5, 5, 2.5}
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
local randNum = math.random()*100
local num = 0
local key
for index, value in ipairs(probability) do
num = num + value
if num > randNum then
key = index
break
end
end
local crateName = crates[math.random(#crates)]
local crate = game.ServerStorage.Crates[crateName]:Clone()
end)
This is by no means the only way to accomplish this, just a way I know that will work. I ran the function 1000 times, these are the crates I got:
Common: 515
Uncommon: 241
Rare: 109
Ultrarare: 69
Epic: 41
Legendary: 25
You can adjust the percentages to your liking, just make sure that randNum can't be bigger than the sum of the probabilities
3 comments
Don't direct answers to a previous question, don't give rewards for help, and don't talk about your "huge plans" -- that's not relevant to you learning
Don't request contact outside of SH in a Question -- if you're using the Q&A service, you get answers here. This isn't an advertising platform.
That is not that is the same site.