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)
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!
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.
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
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!
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
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!