Infinite Yield possible on leaderstats WaitForChild

script.Parent.MouseButton1Click:Connect(function(Player)
	local plr2 = game:GetService('Players')
	local Money = game.Players:WaitForChild("leaderstats"):WaitForChild("Cash")
	if plr2 then
		local Money = plr2:WaitForChild("leaderstats")
		
		while true do
			Money.Value += 5000
		end
		





	end
end)
	

im getting a warning in output and i dont think its running my script even if its not an error but im not good at solving stuff like this. the warning reads

09:32:42.908 Infinite yield possible on ‘Players:WaitForChild(“leaderstats”)’

Hey!
You are trying to access leaderstats via game.Players but they should be in game.Players.LocalPlayer

Hey! i tried adding that in and it gave me

Workspace.Part.SurfaceGui.Frame.TextButton.Script:3: attempt to index nil with ‘WaitForChild’ - Server - Script:3

Should i try a local script?

Ahh, my bad. I thought you were using a local script.

local Money = Player:WaitForChild("leaderstats"):WaitForChild("Cash")

Also I suggest removing this line because it overwrites Money variable:

	local Money = plr2:WaitForChild("leaderstats")

Okay so i ended up changing the script to

script.Parent.MouseButton1Click:Connect(function(Player)
	local plr2 = game:GetService('Players')
	local Money = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")

		if plr2 then
		while true do
			Money.Value += 5000
			wait(1)
		end
    end	
end)

But im still not getting the leaderstat value after ive clicked my surface gui button. oh i also changed everything to a local script

Local Scripts don’t run in Workspace, you need to use Scripts instead.

script.Parent.MouseButton1Click:Connect(function(Player)
	local plr2 = game:GetService('Players')
	local Money = Player:WaitForChild("leaderstats"):WaitForChild("Cash")
    print("It worked!")
	if plr2 then	
		while true do
			Money.Value += 5000
		end
	end
end)

Alr so i changed my code to what u wrote down and its giving me

Workspace.Part.SurfaceGui.Frame.TextButton.Script:3: attempt to index nil with ‘WaitForChild’

Are you leaderstats located in a player? You can check it by running the game in studio and going to explorer → Players → player → and there should be leaderstats

Yup
image

Okay, after some investigation I understand that you can’t use player paramater with MouseButton1Click.
I have found an old topic with a solution:
I’m not sure it’s possible to get the player who clicked the button from a server script - at least not easily.
All you need to do is move the SurfaceGui from the Part which it’s currently in (example workspace.Part), put it into your StarterGui and in the Properties of SurfaceGui you will see one called Adornee - Click on this and then click on the part the gui was originally in (workspace.Part) - now you can use local scripts.

Wow man your a real one. I still get an error but its working how i want it. Tysm.
Heres the error btw

Players.drxvned.PlayerGui.UpgradeGui.Frame.TextButton.Script:3: attempt to index nil with ‘WaitForChild’

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