How to make a textbox have a cap from local player leaderboard stat

  • Pretty much I’m working on a coinflip game where you can gamble (fake money) doing this I ran into 2 big issues, putting negative money and putting more money than the player has.

  • Looking for help on how to make a checker for if the player does not have enough “Money”

  • I have looked at other forum posts along with youtube tutorials saying

local box = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
if player.leaderstats.Money.Value >= box.Text 
end

seeing this check ive tried placing into multiple different lines of code but it always show up X and when it doesn’t it does nothing.

local box = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local textBox = script.Parent
function PositiveIntegerMask(text)
	return text:gsub("%D+", "")
end

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	-- Replace the text with the formatted text:
	textBox.Text = PositiveIntegerMask(textBox.Text)
end)

Before I get any replies about the negative, I already fixed that thanks to some helpful post, just struggling on the checker for player stats

I don’t think its that hard. There are plenty of youtube tutorials for that

Try adding this into your script

local RequirdMoney = XXX  -- Money Required

local Players = game:GetService("Players")
local player = Players.LocalPlayer
if player.leaderstats.Money.Value >= RequiredMoney then
   -- Do stuff
    else
    -- Do stuff

Hello! I’m pretty new to scripting so decided I wanted to tackle this issue. I made a similar GUI to yours and set up a coinflip mechanic. It works fine with the only bug being that since we’re just using If statements if you input all your money it tells you that you don’t have enough but still functions after that line. This can be easily fixed by adding another if statement like so:

Fix

if tonumber(TextBox.Text) == money.Value then
     flip()
     return
end

Put this right below where we check if the entered number is negative

Script

local player = game.Players.LocalPlayer --We define the player
local money = player:WaitForChild("leaderstats"):WaitForChild("Money") --Locate the players money
local TextBox = script.Parent.Parent.TextBox --Define the textbox which we get our input from
local Heads = player.PlayerGui.CoinFlip.MainFrame.Heads --Next 4 are all just defining our GUI that will be edited
local Tails = player.PlayerGui.CoinFlip.MainFrame.Tails
local outcome = player.PlayerGui.CoinFlip.MainFrame.Outcome
local broke = player.PlayerGui.CoinFlip.MainFrame.Broke
local Total = player.PlayerGui.CoinFlip.MainFrame.Total
local heads = Instance.new("IntValue",player) --We create a new IntValue to store the amount of heads, tails, and our total flips.
heads.Name = "Heads"
heads.Value = 0
local total = Instance.new("IntValue",player)
total.Name = "Total"
total.Value = 0
local tails = Instance.new("IntValue",player)
tails.Name = "Tails"
tails.Value = 0
local Text = TextBox.Text 


TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	Text = TextBox.Text
	print(Text)
end)

script.Parent.MouseButton1Click:Connect(function()
	
	print("You clicked the button!") --Ensuring that our button is working
	if tonumber(TextBox.Text) <= money.Value and tonumber(TextBox.Text) <= 0 then --We make sure that the entered number is always positive and make sure to check first to prevent duplication.
		script.Parent.Parent.Parent.MainFrame.Broke.Visible = true
		wait(2)
		script.Parent.Parent.Parent.MainFrame.Broke.Visible = false
		return
	end
	if tonumber(TextBox.Text) >= money.Value then --We check if the amount entered isn't more than our total money count
		script.Parent.Parent.Parent.MainFrame.Broke.Visible = true
		wait(2)
		script.Parent.Parent.Parent.MainFrame.Broke.Visible = false
		return
	end
	if tonumber(TextBox.Text) <= money.Value then --If all is well we run the coinflip function
		flip()
		return
	end

	
end)





function flip()
	
	money.Value = money.Value - tonumber(TextBox.Text) --We take away however much the player bet
	
	local flipResult = math.random(1,2)
	
	if flipResult == 1 then --We define our heads and our tails
		print("Coin landed on heads!")
		total.Value = total.Value + 1
		heads.Value = heads.Value + 1
		Heads.Text = ("Heads: "..heads.Value)
		outcome.Text = "Heads!"
		Total.Text = total.Value
		                        --You can add an outcome for landing on heads here
	end
	
	if flipResult == 2 then
		print("Coin landed on tails!")
		total.Value = total.Value + 1
		tails.Value = tails.Value + 1
		Tails.Text = ("Tails: "..tails.Value)
		outcome.Text = "Tails!"
		Total.Text = total.Value
		                       --You can add an outcome for landing on tails here
	end
	
end

--If you want you can create a seperate boolean value to save the players guess as true or false with you deciding which is heads and which is tails
--this will make the user experience more interactive and give them more gameplay.

I hope this helped you out with your issue. You can either use the whole script or just pieces of it!
Let me know if you find any bugs and I’ll do my best to fix them.

Still having trouble trying to combine everyone’s scripts with my own
coinflip.rbxl (54.8 KB)

here is my game if anyone can use my code and fix my error

Here you go, your whole script was bad variables not defined etc. So I had to rewrite most of it to get it working. Next time instead of doing everything inside the click function just have the if statements within which fire the coin flip if all of the variables were met. Roblox won’t let me upload the file so I uploaded it to mediafire instead.

Link:
https://www.mediafire.com/file/643iuvr9l0ul75a/coinflipgame.rbxl/file

If anything in it isn’t working let me know and I’ll fix it.