How to get a timer using labels. (Begginer Question)


#1

I’ve been trying to create a simple function that makes a timer which increases every second.

This is my script.

function init(self)
	self.st = 0
end

function update(self, dt)
	self.st = self.st + dt
	local time = self.st
	label.set_text("#label", "Survival Time "local time"")
	end
end

But every time I try to build it says I need to add a parenthesis near local. I have tried to reformat it, using self.st itself, adding parenthesis and hash marks but it still won’t even load so I can’t even figure out if there are errors in the rest of my code.


#2
function init(self)
	self.st = 0
end

function update(self, dt)
	self.st = self.st + dt
	local time = self.st
	label.set_text("#label", "Survival Time "..time)
end

#3

Explanation:

  1. The word local should only be used when defining a new local variable (one that is visible in the current scope, basically until the next end), not when using it later on.
  2. Strings are concatenated with the .. operator.

#4

Thanks