'My cash value is suppose to go up every minute but its not. (Roblox Studio lua error)

I was trying to re-edit the code over and over again but it still didn't work I've created the folder leader stats and when I play the game it shows that it's a part of the player. It says however that it isn't a valid member.

The code that causes the error

The other code that causes another error

The error The other error says: Cash is not a valid member of Folder "Players.(players name).leaderstats"



Solution 1:[1]

It's because game.Players.PlayerAdded is an event which you're assigning to a variable.

Try this for the PlayerAdded script (you will need that cash add function in this script):

local players = []

game.Players.PlayerAdded:Connect(function(ply)
    table.insert(players, ply)
end)

while true do
    wait(60)
    for i, v in ipairs(players) do
       v:WaitForChild("leaderstats").Counter.Value += 1
    end
end

I've written this from my memory as I am away from a PC that can test this code so best of luck!

Solution 2:[2]

while wait(60) do
    for i, v in ipairs(game.Players:GetChildren()) do
        if v:FindFirstChild("leaderstats") then
            if v.leaderstats:FindFirstChild("Counter") then
                v.leaderstats.Counter.Value += 1
            end
        end
    end
end

You always need to make sure what you're using exists. If you want to avoid errors, I prefer using FindFirstChild instead of WaitForChild to not get it into an infinite wait incase it somehow doesn't load.

Solution 3:[3]

it looks like you forgot to create the leaerstats folder. Here is a fixed code:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
   local Leaderstats = Instance.new("Folder")
   Leaderstats.Name = "leaderstats"
   Leaderstats.Parent = player

   local Counter = Instance.new("IntValue")
   Counter.Name = "Counter"
   Counter.Parent = Leaderstats

   while true do
       task.wait(60)
       Counter.Value += 1
   end
end)

This will start counting time after player joins. If you want to increase counter value of all players at the same time, use this code:

local PlayersService = game:GetService("Players")

local Players = {}

PlayersService.PlayerAdded:Connect(function(player)
   local Leaderstats = Instance.new("Folder")
   Leaderstats.Name = "leaderstats"
   Leaderstats.Parent = player

   local Counter = Instance.new("IntValue")
   Counter.Name = "Counter"
   Counter.Parent = Leaderstats

   table.insert(Players, player)
end)

while true do
    task.wait(60)
    for _, player in ipairs(Players) do
      player.leaderstats.Counter.Value += 1
    end
end

You don't need to check if "Counter" or "leaderstats" exist as they are created before the player is being inserted into the table.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 HeyHoo
Solution 3