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

How does on go about doing an mathematical operation on a string value?

Asked by 10 years ago

I'm trying to get this script to put a decal on a screen, from a gui textbox, but the output tells me that i cannot do math on a string value. Any help is most appreciated

button = script.Parent
DecalId = script.Parent.DecalId
screen1 = game.Workspace.screen1.Decal.Texture
screen2 = game.Workspace.screen2.Decal.Texture

function onclicked()
    screen1 = "http://www.roblox.com/asset/?id="..button.idInput.Text - 1
    screen2 = "http://www.roblox.com/asset/?id="..button.idInput.Text - 1
end

script.Parent.idInput.MouseButton1Down:connect(onclicked)

2 answers

Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
10 years ago

You have to convert a string to a number to do math on it, use tonumber() :

ExampleString = "300"
print(tonumber(ExampleString) - 100) --Would print 200

So in your case:

screen1 = "http://www.roblox.com/asset/?id="..(tonumber(button.idInput.Text) - 1)
screen2 = "http://www.roblox.com/asset/?id="..(tonumber(button.idInput.Text) - 1)
Ad
Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago

You're concatenating it before you're subtracting one. Put the number in parenthesis with the math. Lua automatically converts numerical strings into numbers when you do math on them.

Answer this question