System that gives gems for playing the game

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a script that automatically gives players gems for playing the game
  2. What is the issue? Include screenshots / videos if possible!
    Every time I run the script it says infinite yield possible.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve read about what an infinite yield is but I still don’t know how to fix it.
local player = game.Players.LocalPlayer
local Gems = player:WaitForChild('leaderstats'):WaitForChild('Gems')



while true do
	wait(1)
	Gems.Value = Gems.Value + 1	
end

can we see the leaderstats script?
also is the script you showed in a server script or a local script?

local PlayerService = game:GetService(‘Players’)

PlayerService.PlayerAdded:Connect(function(plr)
wait(1)
local leaderstats = Instance.new(“Folder”, plr)
leaderstats.Name = ‘leaderstats’

local Gems = Instance.new("IntValue", leaderstats)
Gems.Name = 'Gems'

end)

Yes, it’s in a local scripttttttttt.

So does it actually ever give you any Gems?

No, that’s why I made the post.

yea I don’t think it will if this happens

Infinite yield errors could encounter once and then the script could start running again from yielding.

the point I’m making is it can’t find either the folder or the value

1 Like

Yea my bad lol, do you see if it creates a leaderstat called Gems or a folder called Leaderstats?

It creates a folder called leader stats then a intvalue called gems, inside of the folder.

You can’t use a local script for giving players data. You need to do this in a regular script in ServerScriptService.

You can use game.Players.PlayerAdded:Connect(function(Player) to get the Player on the server.

1 Like

why do you need a wait() in this script?

1 Like

It still has the same error.
The script -

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = player:WaitForChild('leaderstats') 
local Gems = leaderstats:WaitForChild('Gems')


while true do
	wait()
	Gems.Value = Gems.Value + 1 
end

end)

Wait never mind it fixed, thank you!

1 Like