Random string selection not working correctly

I wrote this code in a module script, and this function is concatenated into another string to continue dialogue (if that makes sense)

Other concatenation
ReplyContent = {"Hm... " ..Secrets.selectSecret()..""};

The issue here is that it only selects one of the strings and keeps it as long as the server is running, not cycling between any others. A common solution I saw was this index stuff, but it didn’t solve my issue. Is it because it’s in a module script?

Here’s the code in the module:

local secrets = {
	"There's a vent somewhere around here that leads to some ruins of a forgotten era... at least that's what I've heard!",
	"I heard from a certain someone that there's a hatch in the roof of a certain room... dunno where it goes, but I bet it's something cool.",
	"You might've noticed some buttons around the lobby. I think there's 8 of them. Pressing them all might do something!",
	"I heard there's a graveyard somewhere nearby, but I don't know if the door leading outside can be opened, so I can't get to it.",
	"Far in the distance, I can see a large concrete building littered with alarms. I don't know what it is.",
}

local lastSecret = 0

function secrets.selectSecret()
	local newIndex
	while not newIndex or newIndex == lastSecret do
		newIndex = math.random(1,#secrets)
	end
	lastSecret = newIndex
	return secrets[newIndex]
end

return secrets

Would appreciate help, cheers

1 Like

So every time you use secrets.selectsecret it returns the same thing?

I thought my explanation of the issue was pretty obvious. Yes, the function only selects one string from the table at random but does not select a random one every time the function is called.

In the while loop print lastindex, newindex, and lastindex==newindex

I don’t understand what you mean here. You want me to print lastSecret and newIndex and do whatever lastSecret==newIndex is but how would that help my case? If I’m missing something please forgive me, scripting is not my forte

This is purely for debugging, you will remove it when we are done. Newindex==lastindex returns what you are checking in the while loop, whether or not they are equal to eachother.

Also, can i see the use scenario for this? You are calling the function everytime and not using a static variable for every message right? Just making sure.

print(lastSecret, newIndex)

returned the numbers 0 and 3, nothing more. I’m not seeing the issue, unless I’m script-blind.

Dialogue, every time a dialogue option is selected it’ll call the function to select a random line.

Reply3 = {
				ReplyTitle = "Can you tell me a secret?"; -- Option
				ReplyContent = {"Hm... " ..Secrets.selectSecret()..""}; -- Response

Could you run the dialogue 3 times? I am trying to see if it’s selecting the same thing which means there’s a problem with your conditions or if it isn’t selecting a new one at all.

Hmm, it worked for me. Maybe an issue with your use scenario, the module works fine though.

Also tried to run the function 3 times under the same module (which is how you’d normally use it), still works perfectly fine.

weird, not sure why it’s not working for me. I’m calling this function inside another module, which is the dialogue module, and I’m using that module for another local script which is the main dialogue manager. I dunno if it’s the way they’re ran or if it’s just a me issue.

the selectSecret function is correctly designed to select a random secret from the secrets table, but it’s always generating a new random index without considering the history of previously selected secrets.

this should do.

local secrets = {
	"There's a vent somewhere around here that leads to some ruins of a forgotten era... at least that's what I've heard!",
	"I heard from a certain someone that there's a hatch in the roof of a certain room... dunno where it goes, but I bet it's something cool.",
	"You might've noticed some buttons around the lobby. I think there's 8 of them. Pressing them all might do something!",
	"I heard there's a graveyard somewhere nearby, but I don't know if the door leading outside can be opened, so I can't get to it.",
	"Far in the distance, I can see a large concrete building littered with alarms. I don't know what it is.",
}

local lastSecret = 0

function secrets.selectSecret()
	lastSecret = (lastSecret % #secrets) + 1
	return secrets[lastSecret]
end

return secrets
local secretModule = require(script.secretsModule)

while task.wait(2) do
	print(secretModule.selectSecret())
end

Can you send the other module and localscript?

Since this is a module script,follow the youtube tutorial function about how to operate module scripts

For the random parts,it is a table right,do it like this

Secrets(math.random(1,#Secrtes) – the secrtes is your table

Well that’s what they want, randomly generated secrets, not programmatically calculated ones. The issue is it gives them a non-randomly selected secret.

Use your old script,print the newIndex and see what it says

Reply to me the newindex printing

Didn’t work, it’s just the same issue. No errors or any unexpected behaviours aside from the one(s) already present. Also, this isn’t what I’m looking for and does not select strings randomly like I want it to, it’s just a regular cycle

I know how to operate module scripts, enough to know that this should work. I’m not sure why it doesn’t.

The main local script (manager) is over 300 lines long and the dialogue module is over 1200 lines long, sending it all would be way too much

Your use of the modulo operator simply counts from 1-5, resetting to 1 when it’s at the length of the table, not random string selection

Why are your local scripts that long? A dialogue script should be at most ~300 lines. Well anyway, this is probably a bug with how you’re handling the responses. You are not taking in a new response each time. I’m not sure how to help you without further context.