Ad
Log in to vote
0

Why can't I use math on textlabels, and why does it only work sometimes?

Asked by 1 month ago
I'm making a game about invading territories, and since I'm new to coding, I don't fully know how purchases work yet, and my code may not be very neat or compact, but I'm not concerned about those.

All I'm concerned about it the fact that this one line of code likes to switch between working and not working. Here's the code:

invade.MouseButton1Click:Connect(function() if military.Text > "500000" and countryname.Text == c1.Text then invframe:TweenPosition(UDim2.new(0.318, 0,1.5, 0), "In", "Back",0.8, true) military.Text = "0" budget.Text = budget.Text * 0.5 c1.Visible = false elseif military.Text < "500000" then nation.Text = c1.Text defeat:TweenPosition(UDim2.new(0.318,0,0.088,0), "Out", "Quint",2, true) start.Visible = false end end)

And here's the problematic line:

budget.Text = budget.Text * 0.5

This line only does it's job like 1% of the time, and when it doesn't, it gives me this error:

Players.AbettrWesley.PlayerGui.MenuGui.MenuFrameOutlineShadow.MenuFrameOutline.MenuFrame.PlayButtonOutlineShadow.PlayButtonOutline.PlayButton.PlayButtonMenuClosingScript:183: attempt to perform arithmetic (mul) on Instance and number

Please let me know what this means and how I can fix it... I've been irritated for days now.
0
Convert the text to a number before attempting to perform arithmetic on it. Ascarson4 138 — 1mo
0
u should put the code ina code block so people can help u easier btw ZeroToH3ro 82 — 1mo
0
So it turns out I'm just stupid... there was nothing wrong with my code, it's just that whenever I tested it, I was impatient and decided to test it before my money was actually above zero. Apparently I tried to divide zero in half and that's not really possible. The output just told me exactly what's not happening. AbettrWesley 3 — 1mo

1 answer

Log in to vote
1
Answered by 1 month ago
Edited 1 month ago

Simple answer: Use tonumber() and tostring()

0
xD MattVSNNL 378 — 1mo
Ad

Answer this question