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

[SOLVED] Error while trying to get number from a text box, help?

Asked by 4 years ago
Edited 4 years ago

So, i was making an coin to cash and cash to coin exchange but when i tried to get the number from the text box it says: "attempt to perform arithmetic (add) on number and string." even though i check if its a number when the exchange button is clicked, so i still dont get it... Can you help?

Scripts:

Exchange script (script, server script service):

local cct = game.ReplicatedStorage.remoteEvents.exchange.cointocash
local ccbt = game.ReplicatedStorage.remoteEvents.exchange.cashtocoin
local txtbox = game.StarterGui.ccexhanfe.Frame.COIN.ccframe1.TextBox
local st = game.ReplicatedStorage.ST


cct.OnServerEvent:Connect(function(player, amntD, sta)
    local thisplr = game.Players:findFirstChild(player.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local cash = stats:findFirstChild("Cash")
                local coin = stats:findFirstChild("Coins")
                if (cash~=nil) then
                cash.Value = cash.Value + txtbox.Text
                st.Value = "SUCCESS!"
            end

        end
    end
end)

Button script (local script, starter gui):

local txtbox = script.Parent.Parent.TextBox
local sta = game.ReplicatedStorage.ST

local amntD = txtbox.Text

local cct = game.ReplicatedStorage.remoteEvents.exchange.cointocash




script.Parent.MouseButton1Click:Connect(function()
        if tonumber(txtbox.Text) then
            sta.Value = "WORKING ON IT!"
            wait(3)
            cct:FireServer(amntD, sta)
        else
            sta.Value = "Please enter a number!" 
        end
end)

Dont judge me for using a StringValue, i will fix that soon, its just for test purposes. Also i tried the tonumber(txtbox.Text) that doesnt work either... it says: attempt to perform arithmetic (add) on number and nil

1
txtbox.Text is a String. Can't use it in math Spjureeedd 385 — 4y
0
And you're using StarterGui. Which is not correct actually. Read up about it. You should check the players' PlayerGui Spjureeedd 385 — 4y
0
Then how do i get the number VikkiVuk 74 — 4y
0
I tried the tonumber but that doesnt work either VikkiVuk 74 — 4y
View all comments (3 more)
1
Well you should use tonumber on the text. But you're getting the text the wrong way. Try sending it via the RemoteEvent Spjureeedd 385 — 4y
0
Ok, i will try that. I will let you know if it works or not. VikkiVuk 74 — 4y
0
Oh, it works!! Thank you! VikkiVuk 74 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Solved by: Spjureeedd

Ad

Answer this question