so im working on a custom admin panel for my game and i ran into an issue where the game errors out when i try to add cash to the player
my code:
---------------------------------------------------[ some variables ]
local localplayer = game.Players.LocalPlayer
local button = script.Parent
local panel = button.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
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()
player.leaderstats.Cash.Value += add
end)
the error:
Players.woloexe.PlayerGui.GameUI.AdminButton.LocalScript:31: attempt to index nil with 'leaderstats' - Client - LocalScript:31
Stack Begin - Studio
Script 'Players.woloexe.PlayerGui.GameUI.AdminButton.LocalScript', Line 31 - Studio - LocalScript:31
Stack End - Studio
Use WaitForChild for the panel and cash as well. Sometimes it doesn’t load that fast.
local panel = button:WaitForChild("AdminPanel")
local cash = panel:WaitForChild("Cash")
If not check if your referencing is correct.
EDIT:
If your Cash value in leaderstats is a NumberValue/IntValue you should convert add to a number.
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)
end
end
)
for some reason once i did what u told me to do, the script doesnt do anything when i click it
---------------------------------------------------[ 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
)
local playervalue = panel:WaitForChild("NameBox").Text
local pv = game.Players:FindFirstChild(playervalue)
if pv then
player = pv
end
while add do
wait()
tonumber(add)
end
The while loop isn’t really doing anything to the variable and it’s not needed at all. The conversion can be done every time the button is clicked. For the button to function, you have to make sure the name in the TextBox ( assuming ) matches exactly with a player currently in server.