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)
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
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")
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 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)
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.
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)