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

A error shows up when I am trying to click on a button. Help me?

Asked by
RootEntry 111
6 years ago

So I am trying to make a bank ATM. I made the gui so it is like I have a textbox then 2 buttons. 1 Button says 'Deposit' and the other one says 'Withdraw'. So when I enter a amount and then click on any of those buttons this error shows up:

07:45:56.117 - Players.iiLevelMaker.PlayerGui.ATMGui.Container.withdraw.LocalScript:9: attempt to compare string with number

This is the script for deposit:

local player = game:GetService('Players').LocalPlayer
local gui = player:WaitForChild('PlayerGui'):WaitForChild('ATMGui')
local button = gui:WaitForChild('Container').deposit
local amountbox = gui:WaitForChild('Container').TextBox
local cash = player.leaderstats.Cash
local bank = player.leaderstats.Bank

function MouseClick()
    if cash.Value >= amountbox.Text then
        bank.Value = bank.Value +amountbox.Text
        cash.Value = cash.Value -amountbox.Text
    end
end

button.MouseButton1Click:connect(MouseClick)

This is the script for withdraw:

local player = game:GetService('Players').LocalPlayer
local gui = player:WaitForChild('PlayerGui'):WaitForChild('ATMGui')
local button = gui:WaitForChild('Container').withdraw
local amountbox = gui:WaitForChild('Container').TextBox
local cash = player.leaderstats.Cash
local bank = player.leaderstats.Bank

function MouseClick()
    if bank.Value >= amountbox.Text then
        cash.Value = cash.Value +amountbox.Text
        bank.Value = bank.Value -amountbox.Text
    end
end

button.MouseButton1Click:connect(MouseClick)

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

You need to use the tonumber function so that you can compare the string :)

local player = game.Players.LocalPlayer
local gui = player:WaitForChild('PlayerGui'):WaitForChild('ATMGui')
local button = gui:WaitForChild('Container').withdraw
local amountbox = gui:WaitForChild('Container').TextBox
local cash = player.leaderstats.Cash
local bank = player.leaderstats.Bank

function MouseClick()
    --Convert string to a number datatype
    local amount = tonumber(amountbox.Text)
    if bank.Value >= amount then
        cash.Value = cash.Value +amount
        bank.Value = bank.Value -amount
    end
end

button.MouseButton1Click:Connect(MouseClick) --'connect' is deprecated
0
I will try it :) RootEntry 111 — 6y
1
Did it work? bartekrabit 38 — 6y
1
Damnit goul! xD I saw this in chat and was gonn' answer haha. awfulszn 394 — 6y
0
It worked. RootEntry 111 — 6y
Ad

Answer this question