Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to perform arithmetic (add) on string?

Im trying to make a warning GUI appear when a player doesn't meet the leaderstats requirements when attempting to unlock a door.

local required_points = 8000
local warning = script.Parent.ScreenGui.Warning

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local time_remaining = player.leaderstats.Time.Value
		if player.leaderstats.Time.Value >= required_points then
			if db then
				db = false
				script.Parent.Transparency = 0.5
				script.Parent.CanCollide = false
				wait(3)
				script.Parent.CanCollide = true
				script.Parent.Transparency = 0
				db = true
			end
		else
			print (type(time_remaining))
			print (type(script.Parent.ScreenGui.Time.Text))
			warning.Visible = true
			wait(4)
			warning.Visible = false
			time_remaining = tostring(time_remaining)
			script.Parent.ScreenGui.Time.Text = "You have " + time_remaining + " minutes."
			script.Parent.ScreenGui.Time.Visible = true
			wait(4)
			script.Parent.ScreenGui.Time.Visible = false
		end
	end
end)

script.Parent.ScreenGui.Time.Text = "You have " + time_remaining + " minutes." was the line that had the error

2 answers

✓ Accepted answer

You concatenate in Lua with .. not +, coming from Python it can be confusing!

local required_points = 8000
local warning = script.Parent.ScreenGui.Warning

local db = true
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local time_remaining = player.leaderstats.Time.Value
        if player.leaderstats.Time.Value >= required_points then
            if db then
                db = false
                script.Parent.Transparency = 0.5
                script.Parent.CanCollide = false
                wait(3)
                script.Parent.CanCollide = true
                script.Parent.Transparency = 0
                db = true
            end
        else
            print (type(time_remaining))
            print (type(script.Parent.ScreenGui.Time.Text))
            warning.Visible = true
            wait(4)
            warning.Visible = false
            time_remaining = tostring(time_remaining)
            script.Parent.ScreenGui.Time.Text = "You have "..time_remaining.." minutes."
            script.Parent.ScreenGui.Time.Visible = true
            wait(4)
            script.Parent.ScreenGui.Time.Visible = false
        end
    end
end)

Lua has a bit of different concatenation symbol than other languages, it's not + but .., so adding string together looks like this

local String = "krx" .. "tenaa"

print(String) -- krxtenaa

So line 26 should be

script.Parent.ScreenGui.Time.Text = "You have " .. time_remaining .. " minutes."

The error meant that you are adding onto a string using math symbols, '+' in this case, in Lua that is no no.

2 comments

0

Beat you. ;)

0

:(

This discussion is preserved as it was. New answers, comments, and votes are closed.