Why isn't the IntValue being created when I used Instance.new after I parented it?

This is the function that creates these int-values and parents them to the player:

local function loadAttributes()
	local players = game:GetService("Players"):GetPlayers()
	
	for i=1, #players, 1 
	do
		local player = players[i]

		wait(1/16)

		if(player.Team.Name == "Playing") 
		then
			local deaths = Instance.new("IntValue")
			deaths.Parent = player
			deaths.Value = 0
			deaths.Name = "Deaths"

			local timeSurvived = Instance.new("IntValue")
			timeSurvived.Parent = player
			timeSurvived.Value = 0
			timeSurvived.Name = "SurvivedFor"
		else
			continue;
		end
	end
end

team-creation:


local function loadTeams()
	MusicEvent:FireAllClients("Spasms")
	
	local playing = Instance.new("Team")
	playing.Name = "Playing"
	playing.TeamColor = BrickColor.new("Tr. Red")
	playing.Parent = game:GetService("Teams")
	
	local players = game:GetService("Players"):GetPlayers()
	for i=1, #players, 1
	do
		local player = players[i]
		local isPlaying = player:FindFirstChild("isPlaying")
		
		if(isPlaying.Value == true) 
		then
			player.Team = playing
		else
			continue;
		end
	end
end

When I test it I get the error:

Infinite yield possible on ‘Players.Herchel4:WaitForChild(“SurvivedFor”)’
Infinite yield possible on ‘Players.Herchel4:WaitForChild(“Deaths”)’

And the Value isn’t parented to the player. Why is this occuring?

1 Like

you probably called loadAttributes before the player loaded. try using the Players.PlayerAdded to add values to the player instead

The functions are only called for a BindableEvent, this is for a Minigame game and by the time the intermission ends and minigames begin running (ie the event fires) the player has already well-loaded in.

maybe you used WaitForChild before you fired the bindable event (it shows the warning if it can’t find the child after 5 seconds, but it’s not an error)

Can’t be it either, the Scripts and Functions that used WaitForChild to get these values are all only active if the event fires, or if another event fired by the event fires.

how could the player be part of the Playing team if Playing was created after loadAttributes was called? unless loadTeams isn’t called when the event fires. and can you show more code, specifically where the WaitForChilds are?

loadTeams function came before loadAttributes, and the waitForChilds are here:


local function deathHandler()
	local players = game:GetService("Players"):GetPlayers()
	
	while(1) 
	do
		wait()
		
		for i=1, #players, 1 
		do
			local player = players[i]

			if(player.Team.Name == "Playing")
			then
				wait(1/16)

				local deaths = player:WaitForChild("Deaths")
				if(deaths.Value>3) 
				then
					local rep = game:GetService("ReplicatedStorage")
					local revertCameras = rep.Events:FindFirstChild("RevertPlrCamera")

					revertCameras:FireClient(player)
				end
			else
				break;
			end
			
			local char = player.Character
			local hum = char:WaitForChild("Humanoid")
			local hrp = char:WaitForChild("HumanoidRootPart")

			hum.Died:Connect(function()
				local spawns = workspace.Spawns
				local spawnArray = {spawns.Spawn, spawns.Spawn1, spawns.Spawn2, spawns.Spawn3, 
					spawns.Spawn4, spawns.Spawn5, spawns.Spawn6, spawns.Spawn7, 
					spawns.Spawn8, spawns.Spawn9}

				local selection = math.random(1, 10)
				local selectedSpawn = spawnArray[selection]

				hrp.CFrame = CFrame.new(selectedSpawn.CFrame.X, 
					selectedSpawn.CFrame.Y, 
					selectedSpawn.CFrame.Z)
			end)
		end
	end
end

(The programming below here is local, not server)


local function countDeath()
	wait()
	
	if(plr.Team.Name == "Playing") 
	then
		local deaths = plr:FindFirstChild("Deaths")

		deaths.Value = deaths.Value + 1
	else
		return;
	end
end

local function countTime()
	wait()

	if(plr.Team.Name == "Playing") 
	then
		local survivedFor = plr:WaitForChild("SurvivedFor")

		while(1)
		do
			wait(RATE_OF_TIME)
			
			survivedFor.Value = survivedFor.Value + RATE_OF_TIME
		end
	else
		return;
	end
end

local function handler()
	wait(1/16)
	
	if(plr.Team.Name == "Playing") 
	then
		local deaths = plr:FindFirstChild("Deaths")
		local survivedFor = plr:FindFirstChild("SurvivedFor")
		
		countTime()
		hum.Died:Connect(countDeath)
	else
		return;
	end
end

is it possible that the player wasn’t on the Playing team when loadAttributes was fired? because if just one player doesn’t have the Deaths and SurvivedFor values, it could yield for everyone because it’s in a for loop. also, why is it in a while loop?

Update: The error was because I forgot to connect the function with the event firing lol. It works now.