Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

infinite yield on WaitForChild("PlayerStats")?

local tool = script.Parent

local Settings = tool:WaitForChild("Settings")
local Strength = Settings:WaitForChild("Strength")

local Player = game.Players.LocalPlayer:WaitForChild("PlayerStats")
local PlayerUI = Player:WaitForChild("PlayerGui").PlayerUI

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local CoinClicked = RemoteEvents.CoinClicked

local Debounce = false

tool.Activated:Connect(function()
 	if Player.PlayerStats.CurrentValue.Value < Player.PlayerStats.Storage.Value then
  		if Debounce == false then
   		Debounce = true
   		CoinClicked:FireServer(Strength.Value)
   		wait(0.20)
	   	Debounce = false
  		end
 else
  		if PlayerUI.StorageFull.Visible == false then
			if Debounce == false then
				Debounce = true
				PlayerUI.StorageFull.Visible = true
				wait(0.5)
				Debounce = false
			end
		end
 	end
end)
 

1 answer

Don't worry, infinite yields arent a problem, it's just your script, it is stuck on that line and always trying infinitly to find the given child which is not loaded yet or might not be a thing at all. To prevent this you can use WaitForChild's second argument, which the wait time, and it'll probarly remove the yielding.

local Player = game.Players.LocalPlayer:WaitForChild("PlayerStats", 1)
--1 is just an example put whatever you want

4 comments

0

also how do i solve this "attempt to index a local player (a nil value)

0

which line is it

0

which line is it?

0

Line 8 (local PlayerUI = Player:WaitForChild("PlayerGui").PlayerUI)

This discussion is preserved as it was. New answers, comments, and votes are closed.