Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to perform arithmetic (add) on string?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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)

Ad
Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

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.

0
Beat you. ;) DinozCreates 1070 — 3y
0
:( imKirda 4491 — 3y

Answer this question