Basically I wanted to add something like a coinflip and use a betamount function, so basically someone enters the amount of chips they want to bet and then they flip and they regain it back by 2 or lose it or get a tie, but if I enter a word instead of a number in the textbox it bugs and I can’t use the textbox again. How do I fix it and check if something is a number? This is the script I currently have:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local betamount = tonumber(script.Parent.Parent.BetAmount.Text)
script.Parent.MouseButton1Click:Connect(function()
if buttonpressed then return end
if not buttonpressed then
buttonpressed = true
local dicething = script.Parent.Parent
local plr = game.Players.LocalPlayer
if plr.leaderstats.Chips.Value >= betamount then
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value -betamount
dicething.YourNumber.NumberRolled.Value = math.random(1, 12)
dicething.TheirNumber.NumberRolled.Value = math.random(1, 12)
script.Parent.Parent.YourNumber.Text = tostring(dicething.YourNumber.NumberRolled.Value)
script.Parent.Parent.TheirNumber.Text = tostring(dicething.TheirNumber.NumberRolled.Value)
if dicething.TheirNumber.NumberRolled.Value > dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "You lost!"
elseif dicething.TheirNumber.NumberRolled.Value < dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "You won!"
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount*2
elseif dicething.TheirNumber.NumberRolled.Value == dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "Tie! GG"
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount
end
elseif plr.leaderstats.Chips.Value < betamount then
end
end
wait(3)
buttonpressed = false
end)
I don’t know if there is a function like IsANumber or something but if there is please tell me how to use it.
You can accomplish that by taking the input and using the tonumber() function on it, with the base being 10. For example tonumber(n, 10). If it returns nil then it’s not an integer. Since the value will be entered as a string anyway from the input in most cases.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local inputtext = script.Parent.Parent.BetAmount.Text
local betamount = tonumber(inputtext, 10)
script.Parent.MouseButton1Click:Connect(function()
if buttonpressed then return end
if not buttonpressed and betamount then
buttonpressed = true
print("working")
So it doesn’t print working so that means there is an error there, but what?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local inputtext = script.Parent.Parent.BetAmount.Text
local betamount = tonumber(inputtext. 10)
script.Parent.MouseButton1Click:Connect(function()
if buttonpressed then return end
if not buttonpressed then
if betamount ~= nil then
print("BetAmount is nill")
end
buttonpressed = true
print("working")
local dicething = script.Parent.Parent
local plr = game.Players.LocalPlayer
if plr.leaderstats.Chips.Value >= betamount then
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value -betamount
dicething.YourNumber.NumberRolled.Value = math.random(1, 12)
dicething.TheirNumber.NumberRolled.Value = math.random(1, 12)
script.Parent.Parent.YourNumber.Text = tostring(dicething.YourNumber.NumberRolled.Value)
script.Parent.Parent.TheirNumber.Text = tostring(dicething.TheirNumber.NumberRolled.Value)
if dicething.TheirNumber.NumberRolled.Value > dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "You lost!"
elseif dicething.TheirNumber.NumberRolled.Value < dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "You won!"
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount*2
elseif dicething.TheirNumber.NumberRolled.Value == dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "Tie! GG"
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount
end
elseif plr.leaderstats.Chips.Value < betamount then
end
end
wait(3)
buttonpressed = false
end)
It was in my code, no worries. The check in the if statement was checking if it is not nil, not if it is nil. Which would inverse the function. Here’s the code after fixing it:
if not buttonpressed then
if betamount == nil then
-- Output some error to the user
return
end
-- Do your code
end
betamount:GetPropertyChangedSignal("Text"):Connect(function()
if type(betamount.Text) == "number" then
else
betamount.Text = betamount.Text:gsub('%D+', '')
end
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buttonpressed = false
local inputtext = script.Parent.Parent.BetAmount.Text
local betamount = tonumber(inputtext)
script.Parent.MouseButton1Click:Connect(function()
if buttonpressed then return end
if not buttonpressed then
betamount:GetPropertyChangedSignal("Text"):Connect(function()
if type(betamount.Text) == "number" then
buttonpressed = true
print("working")
local dicething = script.Parent.Parent
local plr = game.Players.LocalPlayer
if plr.leaderstats.Chips.Value >= betamount then
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value -betamount
dicething.YourNumber.NumberRolled.Value = math.random(1, 12)
dicething.TheirNumber.NumberRolled.Value = math.random(1, 12)
script.Parent.Parent.YourNumber.Text = tostring(dicething.YourNumber.NumberRolled.Value)
script.Parent.Parent.TheirNumber.Text = tostring(dicething.TheirNumber.NumberRolled.Value)
if dicething.TheirNumber.NumberRolled.Value > dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "You lost!"
elseif dicething.TheirNumber.NumberRolled.Value < dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "You won!"
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount*2
elseif dicething.TheirNumber.NumberRolled.Value == dicething.YourNumber.NumberRolled.Value then
dicething.Status.Text = "Tie! GG"
plr.leaderstats.Chips.Value = plr.leaderstats.Chips.Value + betamount
end
elseif plr.leaderstats.Chips.Value < betamount then return
end
else
betamount.Text = betamount.Text:gsub('%XD+', '')
end
wait(3)
buttonpressed = false
end
error:
Players.seth20062.PlayerGui.Dice.ScreenGui.Frame.PlayButton.LocalScript:37: Expected ')' (to close '(' at line 8), got <eof>
Alright. This error means there’s something wrong with your code. You forgot to put an ‘)’ closing a statement somewhere or something.
Here’s a simple answer:
type(i)
Returns the type of a value in a string. String are: “number”, “string”, “userdata”, “table”.
So, whenever you need to check, do:
if type(3486338) == "number" then do_something() end
if type({}) == "table" then do_something() end
if type('str') == "string" then do_something() end
if type(newproxy(true)) == "userdata" then do_something() end
It also returns userdata if the object is an Instance. If you use typeof then it works for more values such as Vector3, UDim, UDim2 and CFrame.