Problem with tables, text, and time!

Hello, devs! :happy3:

I’ve been trying to make a splash system for my game that cycles through a table of strings, however one particular splash that concatenates time() isn’t working as expected.

The problem here is that it returns a single fixed value that doesn’t update, even with functions and ModuleScripts.

I searched everywhere, and couldn’t find a solution to this problem.

local splash = script.Parent

local splashes = {
	"Label!",
	"hello world!",
	"You've been playing for "..time().." seconds!", --problem is here, it doesn't update
	"In early alpha!",
	"Sample Text!"
}

splash.Text = splashes[math.random(#splashes)]

Thanks in advance!

1 Like

I think the issue is that you are assuming that time() gets fired every time you reference it in the table. In reality, time() gets fired once when splashes is initialized and does not change, even though the function is part of the string. You would have to have a function that updates the time and changes the string in the table.

1 Like

How though? I’ve tried using a function with a while loop but it didn’t work. Even detecting the splash in question with things such as

if splash.Text == splashes[3] then
   --bla bla bla
end

failed for me. I don’t know what else to do. :sad:

Functions don’t work in tables, at least in the way you are trying to accomplish. You would have to use time() outside of splashes.

using your solution, this is how it’ll look like, but as the other guy says, you would have to use time() outside of the splashes.

local splash = script.Parent

local splashes = {
	"Label!",
	"hello world!",
	"You've been playing for %d seconds!", --problem is here, it doesn't update
	"In early alpha!",
	"Sample Text!"
}

while task.wait(0.5) do
	splash.Text = splashes[math.random(#splashes)]
	
	if splash.Text == splashes[3] then -- it changes the text every time its equal to splashes[3]
		splash.Text = "You've been playing for "..time().." seconds!"
	end
end
1 Like

Not quite what I was looking for, but it works as a nice framework to base myself off of. Thanks!! :woot:

To whoever this may concern, the code I ended up using was:

local splash = script.Parent
local splashes = {
	"Label!",
	"hello world!",
	"You've been playing for %d seconds!",
	"In early alpha!",
	"Sample Text!"
}

local ransplash = splashes[math.random(#splashes)]

local function renderSplash()
	if ransplash == splashes[3] then
		while wait() do
			splash.Text = "You've been playing for "..math.round(time()).." seconds!"
		end
	else
		splash.Text = ransplash
	end
end

renderSplash()

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