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

How do I randomise the math.random number each time? I get the same number each time for an object

Hello,

So I am making a game, in which when you click on the item on the floor, it will disappear between 2-4 minutes. However, when I tested this out, the item was set to invisible for lets say 132 seconds, but once it reappeared, when I pressed it again, it disappeared for 132 seconds.

The question I want to ask is if you can actually make it invisible for a different amount of time each time the item is clicked. So I don't want it to disappear for 132 seconds every time, because for the more "common" items, people can just camp them. I want it to have a random rate but I'm not sure how.

--Variable Definition
local cc = script.Parent
local candycane = game:GetService("ReplicatedStorage").candycane
local cd = cc.ClickDetector
local randomnumber = math.random(120,240)

--Main bit
cd.MouseClick:Connect(function(player)
	local bp = player.Backpack
	if not bp:FindFirstChild('candycane') then
		candycane:Clone().Parent = bp
		
		print(randomnumber) -- this is so I can see how long it's disappeared for. 
		wait(1)
		cc.Handle.Transparency = 1
		wait(randomnumber)
		cc.Handle.Transparency = 0
	else 
		print "already in backpack bro ,what ya doing fam, you stupid boy"
	end
end)

So what I did was i defined math.random(m,n) at the start, then referred to it after I set the handle Transparency = 0

So every time I start running the script again, the time (for example) is 165, and the item is invisible for 165 seconds (after being clicked on), then it is visible again. But when i click on it again, the time it disappears for is 165 again, which I don't want to occur. I want these items to be Legendary, so the spawn rates are random and I will add in a script which will move the object.

However this only happens for one item. So the candy cane I have in this script spawns at 182 seconds (using math.random) but when it reappears, it disappears for 182 seconds, but i want it to disappear for a different number

So can someone help me? Every time I click the item, the number from math.random i get is like (another example) 182, then I want it to generate another random number, and not use 182 again.

Thanks, Tony V

3 comments

1

math.random(n, m) gives you a number. What you're doing is setting a variable to the value of that random number, and then reusing that. Try replacing every occurence of randomnumber with math.random(120, 240).

0

This is the answer. Basically replace every variable that refers to the randomn to math.random(120,240)

0

Thanks. i would have done that but I wnated to also know if there was another way like using math.randomseed((tick) but there's not point going into that much effort

1 answer

✓ Accepted answer

Put math.randomseed(tick()) at the top of your script

randomseed() has 1 argument, which is the given seed. In this case, we input tick(), which is the amount of time that has passed on the client's computer since the UNIX epoch (January 1st, 1970) The key here is that time is never the same in any case, so this is a truly random seed.

randomseed()

tick()

3 comments

1

-1 did not explain what this is doing at all.

0

Yeah you might want to elaborate on how this helps.

0

does that work for u mr. admin

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