My gui button isnt doing anything

so im making a custom admin panel thing on my game and the button for giving money isnt working

---------------------------------------------------[ some variables ]

local localplayer = game.Players.LocalPlayer
local button = script.Parent
local panel = button:WaitForChild("AdminPanel")

---------------------------------------------------[ open/close logic ]

if localplayer.Name == "woloexe" then
	button.Visible = true
else
	button.Visible = false
	panel.Visible = false
end

function onButtonPressed()
	panel.Visible = not panel.Visible
end
button.MouseButton1Click:Connect(onButtonPressed)

---------------------------------------------------[ even more variables ]

local playervalue = panel:WaitForChild("NameBox").Text
local pv = game.Players:FindFirstChild(playervalue)
if pv then 
	player = pv
end

local add = panel.Cash:WaitForChild("AddCash").Text
while add do
	wait()
	tonumber(add)
end

local deduct = panel.Cash:WaitForChild("DeductCash").Text
local set = panel.Cash:WaitForChild("SetCash").Text

local addc = panel.Cash:WaitForChild("AddCash").Confirm
local deductc = panel.Cash:WaitForChild("DeductCash").Confirm
local setc = panel.Cash:WaitForChild("SetCash").Confirm

local kick = panel.Moderation.Kick
local ban = panel.Moderation.Ban

----------------------------------------------------[ admin logic ]

addc.MouseButton1Click:Connect(function()
	local playervalue = panel:WaitForChild("NameBox").Text
	local pv = game.Players:FindFirstChild(playervalue)
	if pv then
			pv.leaderstats.Cash.Value += add
	end
end)
1 Like

Two things that I’ve noticed is:

  1. You have a unneeded while loop that isn’t wrapped in a spawn or coroutine. This means anything below the while loop won’t run. What you need to do is either wrap it in a coroutine, or place it at the end of the script, OR what I suggest is instead of constantly converting the “add” variable into a number. You just convert it whenever you are giving the “cash”. This brings me to the second point!

  2. You’re trying to change the cash value on the client. This will “look” like it has worked (for you only) but in reality it hasn’t and the server won’t register any additional cash given or removed. What you need to do is use remote events to communicate with the server. Basically, when you click the button to give cash, you will have it fire a remote event with the “tonumber(add)” and the player your giving cash to as arguments. On the server you listen for that remote to be fired → check if the person who fired it is legit (as in an admin) and lastly you would then give the player cash (basically the code thats currently in your mouseButton1Click connection.

1 Like

oh i already know that the cash isnt fe, im just trying to get it to work locally before i start making remote events and such for it to be server-side

Just making sure! Then your issue should just be the while loop (view my previous post for solutions you can try).

i tried removing the while loop and now it feels like the script is ignoring the tonumber() i wrote

i moved it to the clicked function and everything

You don’t need the while loop. All you need to do is use tonumber whenever you want to add cash or send the cash as a argument.

So in your current code all you would do is remove the while loop and change click function to this:

addc.MouseButton1Click:Connect(function()
	local playervalue = panel:WaitForChild("NameBox").Text
	local pv = game.Players:FindFirstChild(playervalue)
	if pv then
			pv.leaderstats.Cash.Value += tonumber(add) -- now its converted into a number!!
	end
end)

Sidenote: When you implement your remotes, just send tonumber(add) as an argument!

ohh i added tonumber() on its own line before adding the cash. could that have been the issue?

wait its still giving me the issue where it thinks the add variable is a string

edit: i just noticed it says nill not string

Players.woloexe.PlayerGui.GameUI.AdminButton.LocalScript:47: attempt to perform arithmetic (add) on number and nil  -  Client  -  LocalScript:47
  Stack Begin  -  Studio
  Script 'Players.woloexe.PlayerGui.GameUI.AdminButton.LocalScript', Line 47  -  Studio  -  LocalScript:47
  Stack End  -  Studio

Change

local add = panel.Cash:WaitForChild("AddCash").Text

to

local add = panel.Cash:WaitForChild("AddCash")

and then when adding the cash just do

			pv.leaderstats.Cash.Value += tonumber(add.Text) -- get the textbox content here instead of before (which might have been nil)

I’m assuming that this is a textbox.

2 Likes

okay so that solved the issue. thank you so much! now im going to make the script do this on a remote event

ALSO make sure that you have an if statement before you add cash that checks if "add.Text ~= ‘’ " as it might be that an empty string returns nil when trying to use to number!

Glad it worked but make sure to add the if statement just incase and goodluck!!!

oh ok ill go and do that now .