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

Why does the math.random function get weird for me?

So I'm tryna make a jojo game and finally for the love of god made quotes working, so now I made multiple and put them all in a data table and used the math.random function to randomly make them play each time the remote event is fired. However, sometimes the remote event fires but the quote and sound doesn't play. It's weird and I don't know why it's happening. Here's the scripts.

Client script =

local player = game.Players.LocalPlayer -- our player
local userinputservice = game:GetService("UserInputService") 
local enabled = false -- begone spam
local Quote = script.Parent:WaitForChild("Quote")

userinputservice.InputBegan:Connect(function(Input, GPE) 
	if not GPE then
		if Input.KeyCode == Enum.KeyCode.N and enabled == false then
			enabled = true 
			Quote:FireServer()
			wait(7)
			enabled = false
		end
	end
end)

Server script =

local remote = script.Parent:WaitForChild("Quote")

remote.OnServerEvent:Connect(function(player)
	print("large joe")
	if math.random(1,3) == 1 then
	local Billboard = script:WaitForChild("BillboardGui"):Clone()
	local sound = Billboard.Sound
	Billboard.Parent = player.Character.Head
	sound:Play()
	wait(6.5)
	Billboard:Destroy()
	elseif math.random(1,3) == 2 then
		print("joe larg")
		local Billboard2 = script:WaitForChild("BillboardGui2"):Clone()
		local Sound = Billboard2.Sound
		Billboard2.Parent = player.Character.Head
		Sound:Play()
		wait(6.5)
		Billboard2:Destroy()
	elseif math.random(1,3) == 3 then
		local Billboard3 = script:WaitForChild("BillboardGui3"):Clone()
		local Sound = Billboard3.Sound
		Billboard3.Parent = player.Character.Head
		Sound:Play()
		wait(5.2)
		Sound:Stop()
		wait(1.5)
		Billboard3:Destroy()
	end
end)

(btw these were put in StarterPack)

Is there anyway I can fix this?

3 comments

0

The reason is because on line 12,20 and 5, the math.random changes for each elseif, store math.random in a variable so it doesnt pick a new random number each time.

0

I tried that but now it just keeps on doing the same quote over and over again. It's not doing it randomly like it's supposed to.

0

You still have to store the result inside the function, so that it changes every time

3 answers

Try storing your math.random in a variable. The reason is:

  1. If you are doing math.random function as a condition for each if statement, there's a possible chance for it to not generates the required number causing nothing to happen.

If you don't get what I am trying to say:

-- example
if math.random(1, 3) == 1 then -- only runs when it generates 1
    print("yay1")
elseif math.random(2, 3) == 1 then -- only runs when it generates 2
    print("yay2")
end

-- if none of the reqs are met then nothing will happen

Instead of doing math.random in each if statement you want to do:

local chosenNumber = math.random(1, 3) -- 1 to 3

if chosenNumber = 1 then
    -- bla bla bla, your code here
elseif chosenNumber = 2 then
    -- bla bla bla, your code here
elseif chosenNumber = 3 then
    -- bla bla bla, your code here
end

Also, there's a chance for it to generates the same number in a row, because it's only 33% ( 1 out of 3 ) chance for each number to be generated.

2 comments

0

Alright, now the thing is, it still does the thing as before, here's a video. It just does the same one for that life. https://streamable.com/ktax80

0

atmxe I told you to put the variable inside your function, if you put it in the beginning it will only run once

I tried putting the math.random variable inside of the onserverevent and now it works.

I tried putting the math.random variable inside of the onserverevent and now it works.

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