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

Help on previous question crates else if?

I have so many questions this project I abandoned by I really want to head back to it but I need some really good explaining I have discord skype if it needs to come to that I need to know a few things

  • what is the difference between else if and elseif

  • math.randomseed something like that or math.random is based off a seed??

  • as I remember I also had problems with the tables some where please help it wi.ll mean a lot and I will for sure put your name in the game I have huge plans for this game

here is the link to the previous question you can comment there please?

https://scriptinghelpers.org/questions/36070/legendary-crate-only-it-wont-change-to-any-other-crate-help

3 comments

0

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

0

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.

0

That is not that is the same site.

1 answer

###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()
	-- Do what you wish with the crate
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()
	-- Do what you wish with the crate
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

6 comments

0

What does tick do I checked in the wiki and I really dont understand I know its not a requesting site but can you show me an example using my script dont change all of them just once because this is my idead math.randomseed(tick()) but what is its uses the wiki said its a time using but what Im trying to do is choose 1 out of the 6 crates like to select them for then randomiser which gives you a c

0

@BlackJPI

0

See my edit

0

kk thanks

0

@BlackJPI Um ok so just one more question for the for i, v in pairs or what ever is the num representing the index just asking I should no this but I dont like is the num the value of index if you understand what I mean

0

nvm I understand now

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