I was wondering how to make a math.Random function run twice.
It picks a random number as intended the first time. However, when I make the function run a second time it does not select a new number.
Make so either at the start or at the end, wherever you keep your result of math.random, aka the variable, set the variable again to a random number
Say you have
local result = math.random(1,10)
at the end of the function, do
result = math.random(1,10)
That didn’t seem to fix it.
Here is the full script if that helps:
local sadasd = 1
game.Workspace.MapSelect:GetPropertyChangedSignal(“Transparency”):Connect(function() – Looks for changes in the Transparency
if game.Workspace.MapSelect.Transparency == 1 then
game.Workspace.MapSelect.Transparency = 0
local RandomMap = math.random(1,2)
if RandomMap == 1 then
game:GetService(“ReplicatedStorage”).DesertEvent:FireServer(sadasd)
print(“Desert Temple”)
elseif RandomMap == 2 then
game:GetService(“ReplicatedStorage”).HomesEvent:FireServer(sadasd)
print(“Town of Nostalgia”)
end
RandomMap = math.random(1,2)
end
end)
The Transparency is set back to 1 from another Script and that works perfectly fine.
Try putting local RandomMap = math.random(1,2)
outside of the event and see if that does anything
That didn’t seem to do anything either.
Hmm, are you sure it’s not working or could it be that it generated the same number as before? Like say it generated 1 first, it could’ve generated 1 again
I have it print the result into the output and nothing gets printed.
It doesn’t get printed the first time or the second time?
It doesn’t get printed the second time or any time after.
It prints the first time.
local twiceRandom do
local previous: number = 0;
local count: number = 0;
local rand: Random = Random.new();
function twiceRandom(numA: number, numB: number): number
numA = numA and numA or 0;
numB = numB and numB or 1;
if (count > 0) then
count = 0;
return previous;
end
count += 1;
local r: number = rand:NextInteger(numA, numB);
previous = r;
return r;
end
end
for i = 1,10 do
print(twiceRandom(1, 50));
end
Try this. This function runs random once, stores the result, the next run it returns the same result. Then the next result a different result, the cycle repeats.
That just made it not work at all.
What are you trying to do exactly?
It’s a Map Voting system that selects a random to be put up on the board instead of the same 2 maps every time.
I sent the script in another reply.
Try this and see what prints?
local sadasd = 1
local MapSelect = workspace.MapSelect
local RepStorage = game:GetService("ReplicatedStorage")
MapSelect:GetPropertyChangedSignal("Transparency"):Connect(function()
print(MapSelect.Transparency)
if MapSelect.Transparency == 1 then
print("Choosing random map")
MapSelect.Transparency = 0
local RandomMap = math.random(1, 2)
if RandomMap == 1 then
RepStorage.DesertEvent:FireServer(sadasd)
print("Desert Temple")
elseif RandomMap == 2 then
RepStorage.HomesEvent:FireServer(sadasd)
print("Town of Nostalgia")
else
print("Ok what")
end
end
end)
So it seems like the randomizer is not the issue and the issue is the function itself is not repeating.
U should call the function twice,
EDIT: i tried in studio, yes u can
> function asd(s) -- I suggest adding a wait or adding a bool value that makes sure it called itself only twice
> print(s)
> asd(s)
> end
>
> asd("ASD")
EDIT: Once Again, here is a easier way to understand it
> local StopNumber = 0 -- stops when it reaches a ceartin number
>
> function asd(s)
> StopNumber += 1
> if StopNumber > 10 then -- stops if its over 10
>
> else
> print(s) -- does the function
> asd(s) -- calls its self again
> end
> end
>
> asd("ASD") -- calling the function
Blockquote