While loop not running

Hey guys, I’ve been working with this while loop inside of a player added function and it simply doesn’t run. I tried to find solutions online but I haven’t found anything. no loops or functions work at all. I replaced the while loop with Runservices’ Heartbeat and it doesn’t run at all. any solutions? Thanks.

game.Players.PlayerAdded:Connect(function(plr)
	print("DataLoaded")
	local PlayerGUI = plr.PlayerGui:WaitForChild("NoteGuis")
	local Char = plr.CharacterAdded:Wait()
	local RootPart = Char:WaitForChild("HumanoidRootPart")
	

	while wait() do
		if (RootPart.Position - script.Parent.Note.Position).Magnitude <=9 then
			PlayerGUI.Interact.Visible = true
		else
			print("Player is to far")
		end
	end
	
end)
3 Likes

I’m not sure if it will work, but try it

game.Players.PlayerAdded:Connect(function(plr)
	print("DataLoaded")
	local PlayerGUI = plr.PlayerGui:WaitForChild("NoteGuis")
	local Char = plr.CharacterAdded:Wait()
	local RootPart = Char:WaitForChild("HumanoidRootPart")
	
    wait(0.5)
	while wait() do
		if (RootPart.Position - script.Parent.Note.Position).Magnitude <=9 then
			PlayerGUI.Interact.Visible = true
		else
			print("Player is to far")
		end
	end
	
end)

edit:
just add wait(0.5) before the loop
The loop started working for me after this

2 Likes

You made the assumption that the character hasn’t existed yet on PlayerAdded. Sometimes this isn’t true, especially when testing in studio. If the character existed already, CharacterAdded won’t trigger, and your code will sit and wait forever. You’ll find out that your code suddenly works if you reset your character.

Anything related to characters and player addeds, you need to code to catch both cases where the character has already existed and has not existed.

game.Players.PlayerAdded:Connect(function(plr)
	print("DataLoaded")
	local PlayerGUI = plr.PlayerGui:WaitForChild("NoteGuis")
	local Char = plr.Character or plr.CharacterAdded:Wait() -- if character doesn't exist, default to event :Wait()
	local RootPart = Char:WaitForChild("HumanoidRootPart")
3 Likes

just tried it and still nothing.
for more context, the location of the script is inside of a part in the workspace

1 Like

I also just made this edit and again, no change. Thanks though.

edit: i reset my character with the change and made it print the magnitude, but instead of constantly updating, it just prints the same mag over and over.

I will try to fix this loop :slight_smile:

1 Like

I removed the local PlayerGui and it started working without any problem

game.Players.PlayerAdded:Connect(function(plr)
	print("DataLoaded")
	--local PlayerGUI = plr.PlayerGui:WaitForChild("NoteGuis") 
	local Char = plr.Character or plr.CharacterAdded:Wait()
	local RootPart = Char:WaitForChild("HumanoidRootPart")

	
	while wait() do
		if (RootPart.Position - script.Parent.Note.Position).Magnitude <=9 then
			print("I not have gui")
		else
			print("Player is to far")
		end
	end

end)

check if the gui is not nil

Question out of curiosity… why aren’t you just using a ProximityPrompt for this? It’s pretty much what they’re for.

The Reason is, I wanted it so that when your in a certain range a Text label pops up. I was thinking of using a proxyprompt but I decided I wanted to try this out.

Thank you, this worked great, now I just have to figure out the gui problem.

you can do it this way:

local PlayerGUI = plr.PlayerGui:WaitForChild()
1 Like
game.Players.PlayerAdded:Connect(function(plr)
	print("DataLoaded")
	local PlayerGUI = plr.PlayerGui:WaitForChild("NoteGuis")
	plr.CharacterAdded:Connect(function()
         local RootPart = Char:WaitForChild("HumanoidRootPart")
         while wait() do
		   if (RootPart.Position - script.Parent.Note.Position).Magnitude <=9 then
			  PlayerGUI.Interact.Visible = true
		   else
			  print("Player is to far")
		   end
	    end
    end)
end)
1 Like

Thanks again, worked like a charm

2 Likes

oh sorry just realized it was fixed already

1 Like