Values are not a valid members of Player when they are

Hello! So i want to create a game similar to cookie clicker but instead of cookies there are balls, but im having a little problem.

So im trying to make a passive income that generates balls every second but there is a bug. Here are the scripts (they are all in ServerScriptService):

add Balls value inside the player-

game.Players.PlayerAdded:Connect(function(plr)
	local Balls = Instance.new("IntValue", plr)
	Balls.Name = ("Balls")
	Balls.Value = 1
end)

add BPS (Balls Per Second) value inside the player-

game.Players.PlayerAdded:Connect(function(plr)
	local BPS = Instance.new("IntValue", plr)
	BPS.Name = ("BallsPerSecond")
	BPS.Value = 1
end)

Passive income script-

game.Players.PlayerAdded:Connect(function(plr)
local BPS = plr.BPS
local Balls = plr.Balls
	
while true do
		wait(1)
		Balls.Value = Balls.Value +(BPS)
	end
end)

So the bug is that when i playtest the game, console says that Balls and BPS values are not a valid member of a player, when they are. This could be caused by studio loading this script first before the scripts that add Balls and BPS values inside the player. So is there any way i can pause the passive income script for it to wait for other scripts?

Yes, that indeed is the issue. Why not add the while loop to the script where you create the value instead of making another PlayerAdded event. The script should look something like this,

game:GetService('Players').PlayerAdded:Connect(function(plr)
	local BPS = Instance.new("IntValue", plr)
	BPS.Name = ("BallsPerSecond")
	BPS.Value = 1
    while true do task.wait(1)
       BPS.Value += 1
    end
end)

ok this works like a charm thank you so much

How I handle waiting for values/attribute values to load on the client is to use a loop, repeatedly checking for the value.:

local BPS
while not BPS do
	BPS = plr.BPS
	task.wait(0.1)
end
local Balls
while not Balls do
	Balls = plr.Balls
	task.wait(0.1)
end

It’s not advisable to use fixed task.wait values, as you cannot predict how long it will take for the value to be created on the server and to then replicate to the client.