Script not working

Hey there, I am trying to create a Roblox game for some experience, and it’s mostly about effecting GUI with code, as well as adding numbers to variables, etc.

I have made this script:

local tax = 10
local taxed = false
local balance = 0
local guitext = game.StarterGui.Cash.TextBox

function taxed()
	if balance > 0 then
		wait(60)
		balance = balance - tax
		taxed = true
		wait(5)
		taxed = false
	end
end

function money()
	wait(10)
	balance = balance + 10
end

function changegui()
	if balance >= 0 then
		guitext.Text = "CASH: " + string(balance)
	else
		guitext.Text = "CASH: 0"
	end
end

while true do
	changegui()
	money()
	taxed()
end

And it does not seem to be working. I was thinking it could be the while true loop, but if it isn’t, why isn’t my code effecting anything in my game? I’ve looked at tutorials to attempt to effect the GUI, but it doesn’t seem to be working.

I’ve been thinking that my code might be pseudo code, but I am not getting any errors in the editor.

I just need someone to point out the problem and help me out a little, thanks!

1 Like

try this:

local Data = {
 ["Tax"] = 10,
 ["Taxed"] = false,
 ["Balance"] = 0,
}
local ps = game:GetService("Players")
local lp = ps.LocalPlayer
local pgui = lp.PlayerGui or lp:WaitForChild("PlayerGui")
local Cash = pgui:WaitForChild("Cash")
local TextBox = Cash:WaitForChild("TextBox")

function Tax(amount)
 if Data.Balance > 0 then
  wait(60)
  Data.Balance = Data.Balance - (amount or Data.Tax)
  Data.Taxed = not Data.Taxed
  wait(5)
  Data.Taxed = not Data.Taxed
end
function GiveCash(amount)
 wait(10)
 Data.Balance = Data.Balance + (amount or 10)
end
function changegui()
	if Data.Balance >= 0 then
		TextBox.Text = "CASH: " .. tostring(balance)
	else
		TextBox.Text = "CASH: 0"
	end
end
while wait() do
 changegui()
 GiveCash()
 Tax()
end
2 Likes
local guitext = game.StarterGui.Cash.TextBox

This isn’t where player guis are stored you have to get the LocalPlayer from the Players service.
ex

local Players = game:GetService("Players") // Where every player object is stored
local localplayer = Players.LocalPlayer // Grab the client that has the guis
local textbox = localplayer.PlayerGui.Cash.TextBox // PlayerGui is where the Players guis are stored

Also you can’t use the add symbol to concatenate strings you need 2 periods(’…’) and it should be ‘tostring()’ not ‘string()’

 TextBox.Text = "CASH: " .. tostring(balance)
1 Like

It worked, but when I use it in game my GUI is displaying nil instead of the value, with no errors.

replace balance with Data.Balance I forgot to change it

Thanks, that will for sure help me with understanding the code a lot better.

It works now, I’ll mark your post with the code as the solution, thank you!

while true do
	changegui()
	money()
	taxed()

    wait()
end

FYI: make sure when using while loops, add a wait() inside it so it doesn’t crash on you, causing the script to not run at all.