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

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

Asked by 5 years ago
Edited 5 years ago

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

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). fredfishy 833 — 5y
0
This is the answer. Basically replace every variable that refers to the randomn to math.random(120,240) Formidable_Beast 197 — 5y
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 tonyv537 95 — 5y

1 answer

Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your 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()

1
-1 did not explain what this is doing at all. User#5423 17 — 5y
0
Yeah you might want to elaborate on how this helps. EpicMetatableMoment 1444 — 5y
0
does that work for u mr. admin Gey4Jesus69 2705 — 5y
Ad

Answer this question