Can not get my leaderstats value change to work

I have a script which i want to repeat every 5 seconds . The script adds the value of 1 to my IntValue which is in leaderstats which is in the Owner . The owner is a module script which contains the players who owns the tycoon in my game . The default name for Owner is “Q” just because I could not think of anything .

Here is the script :

Owner = require(script.Parent.Parent.TheOwner)

while true do
if Owner.player.Name ~= “Q” then
Owner.player:FindFirstChild(“leaderstats”).Dreams.Value =
Owner.player:FindFirstChild(“leaderstats”).Dreams.Value + 1
end
end

Here is the module script :

I have added a button in the game which changes the name “Q” in the module script to the person who clicked its name . So the if statement should run but then it says i called a nil Value .

1 Like

Maybe try-

while wait(5) do
local owner = game.Players:FindFirstChild(Owner.player)

if owner then
local leaderstat = owner:FindFirstChild("leaderstats") and owner.leaderstats:FindFirstChild("Dreams")

if leaderstat then
leaderstat.Value = leaderstat.Value + 1
end
end
end
1 Like

Oh thank you . This worked . I see what i did wrong now .

1 Like

It’s not, it’s just the way the DevForum has formatted it:

If it was two separate statements, neither would make sense alone in this context as they would need to be assigned a value.

Also, while it’s true that the while loop isn’t very efficient (I have no idea why it’s being used in this case anyways), a wait() shouldn’t be necessary if reasonable conditions are used in the loop.