Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

4 comments

0

I will try it :)

1

Did it work?

1

Damnit goul! xD I saw this in chat and was gonn' answer haha.

0

It worked.

This discussion is preserved as it was. New answers, comments, and votes are closed.