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)]
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.
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
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()