Hello, i have an in pairs loop that loops through buttons in a UI and then when a button is clicked, it will get the name of that button, then it will check the leaderstats if there is something with the same name as that button and change it. but i cant seem to figure out how to do this.
here is my code:
for i, Button in pairs(Frame:GetChildren()) do
if Button:IsA("TextButton") then
Button.MouseButton1Click:Connect(function()
if player.leaderstats.(Button.Name).Value <=0 then
return nil
end
for i, Button in pairs(Frame:GetChildren()) do
if Button:IsA("TextButton") then
Button.MouseButton1Click:Connect(function()
if player.leaderstats:FindFirstChild("Button.Name").Value <= 0 then
return end
end
end)
end
end
local Frame --Your frame path here
local player = game.Players.LocalPlayer
for _, Button in ipairs(Frame:GetChildren()) do
if Button:IsA("TextButton") then
Button.MouseButton1Click:Connect(function()
if player.leaderstats:FindFirstChild(Button.Name).Value <= 0 then
return nil
end
end)
end
end
You first need to check if a leaderstats with the same name exist because if it doesn’t it will give an error.
local player = game.Players.LocalPlayer
for _, Button in ipairs(Frame:GetChildren()) do
if Button:IsA("TextButton") then
Button.MouseButton1Click:Connect(function()
if player.leaderstats:FindFirstChild(Button.Name) then
if player.leaderstats[Button.Name].Value <= 0 then
return nil
else
-- leaderstats exists and the value is greater than 0
end
end
end)
end
end