Leaderstats returns to default value instead of it's current when i add a value to it

  1. What do you want to achieve? Add 30 to a leaderstat value every 3s if the object’s parent is Workspace
  2. What is the issue? It adds 30 to the starting leaderstat value instead of the current, so if 1000 is the default value and 300 is the current, it will make the leaderstat 1030 instead of 330

My script inside an object

game.Players.PlayerAdded:Connect(function(player)
	while true do
		wait(3)
		if script.Parent.Parent == game:GetService("Workspace") then
			local Funds = player:WaitForChild("leaderstats").Funds
			Funds.Value += 30
		end
	end
end)
2 Likes

Does this script Client or Server side?
If the default value is 1000 (Server) and you changed it to 300 (On the client), and this script is Server, It might make sense.

2 Likes

You’re not tying the increment function to the specific player who joined. Your script loops endlessly when any player joins, but it keeps using the same ‘player’ variable—it isn’t checking for which player owns the parent object.

Here’s an example to explain this:

  • A player joins the game named Player1. Your code begins running in an endless loop using the variable ‘player’, which equals Player1.
  • Another player named Player2 joins. Since the code is already running in loop for Player1, it has no effect on Player2’s stats.
  • Your code detects that your object’s parent is the Workspace and wants to add 30 to the player’s funds. However, it can only add funds to Player1 because ‘player’ only equals Player1.

So how do we resolve it? The best method would be to add a variable in the object the script is in (let’s call it ‘Owner’). When the parent of your object becomes the Workspace, you’ll want to fetch the ‘Owner’ value and add funds to their stats.

Here’s a fix:

game:GetService("Workspace").ChildAdded:Connect(function(child)
    while true do
        wait(3)
        if child == script.Parent and child:FindFirstChild("Owner") then 
            local player = game.Players:FindFirstChild(child.Owner.Value)
            if player then
                local leaderstats = player:FindFirstChild("leaderstats") or Instance.new("IntValue")
                leaderstats.Parent = player
                leaderstats.Name = "leaderstats"
            
                local Funds = leaderstats:FindFirstChild("Funds") or Instance.new("IntValue")
                Funds.Parent = leaderstats
                Funds.Name = "Funds"
                Funds.Value += 30
            end
        end
    end
end)
1 Like

In case if its unclear for you, create the ‘Owner’ variable as a StringValue inside the object and set its value to the name of the player who owns it. Also make sure the child object is owned by a player. Their name mustmatch exactly with the name stored in ‘Owner’.

1 Like

This is indeed what i did. Do you know a fix?

1 Like

The game is single player so specific player things dont really matter

2 Likes

Ok, try this instead:

game.Players.PlayerAdded:Connect(function(player)
	if script.Parent.Parent == game:GetService("Workspace") then
		local leaderstats = player:WaitForChild("leaderstats")
		local Funds = leaderstats:WaitForChild("Funds")
		while true do
			wait(3)
			Funds.Value += 30
		end
	end
end)
1 Like

You could simply make a script to control all of those (Called a system)!

Server Script

local Players = game:GetService("Players")
local DefaultFundsValue = 1000

Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Folder", Player)
    leaderstats.Name = "leaderstats"

    local Funds = Instance.new("IntValue", leaderstats)
    Funds.Name = "Funds"
    Funds.Value = DefaultFundsValue
end)

–Changing this script to a normal (Server) script

game.Players.PlayerAdded:Connect(function(player)
	while true do
		wait(3)
		if script.Parent.Parent == game:GetService("Workspace") then
			local Funds = player:WaitForChild("leaderstats").Funds
			Funds.Value += 30
		end
	end
end)
1 Like

This didn’t change anything sadly
I am thinking of instead of removing cash in a local script using a RemoteEvent

1 Like

This was the issue, the leaderstats was changed to 300 in a local script. Instead, I used a RemoteEvent.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.