Math.random help

function getRandomDialogue()
	local dialogue = Dialogue[math.random(#Dialogue)]
	if dialogue ~= LastDialogue1 and dialogue ~= LastDialogue2 and dialogue ~= LastDialogue3 and dialogue ~= LastDialogue4 and dialogue ~= LastDialogue5 and dialogue ~= LastDialogue6 then
			LastDialogue6 = LastDialogue5
			LastDialogue5 = LastDialogue4
			LastDialogue4 = LastDialogue3
			LastDialogue3 = LastDialogue2
			LastDialogue2 = LastDialogue1
			LastDialogue1 = dialogue		
	end
	return dialogue;
end

This is supposed to prevent the same dialogue from being said. For some reason, it will apply the same strings to itself and still repeat strings back to back if lucky enough. Any help appreciated.

math.random uses two parameters, a min and a max, try math.random(1, #Dialogue).

Edit: my bad, I am unfamiliar with lua math functions.

math.random(n) is the same as math.random(1, n). When only one argument is passed, it starts from 1 by default

5 Likes

Exact same results as before

charsssssss

may try this it should cycle through use each one 1 time if i have it right then reset after all is used to do it again

local UsedDialogues = {}
function getRandomDialogue()
	local dialoguekey = math.random(#Dialogue)
	repeat 
		task.wait()
	until not table.find(UsedDialogues,dialoguekey)
	table.insert(UsedDialogues,dialoguekey)
	if #UsedDialogues == #Dialogue then  -- this will reset the table so they can all be randomly used again
		UsedDialogues = {}
	end
	local dialogue = Dialogue[dialoguekey]
	
	return dialogue;
end

or maybe this it will allow the dialogues to be said randomly without repeating the same one back to back

local LastDialogue
function getRandomDialogue()
	local dialoguekey = math.random(#Dialogue)
	repeat 
		task.wait()
	until LastDialogue ~= dialoguekey
	LastDialogue = dialoguekey
	local dialogue = Dialogue[dialoguekey]
	return dialogue;
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.