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

While true do loop in a script?

Hi guys! The while loop is not working, but everything else works! :( I can't think of any other way to get the while loop to work :/ Any ideas?

local LocalPlayer = game:GetService("Players").LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait()

LocalPlayer.PlayerModel.PetEquipped.Changed:connect(function()
 if LocalPlayer.PlayerModel.PetEquipped.Value == "Dog" then
Pet = game.ReplicatedStorage.Dog:Clone()
Pet.Parent = Character
Pet.Name = "Pet"
Pet.CanCollide = false
Pet.Transparency = 0.25 -- set transparency
Pet.Size = Vector3.new(2, 2, 2) --set size here :P
BodyPosition = Instance.new("BodyPosition", Pet)


while wait(0) do
	BodyPosition.Position = Character.Head.CFrame:pointToWorldSpace(Vector3.new(2, 1, 0))
end
end
end)


Thanks!

2 answers

I suggest that you learn to tab and organize your code better.

By just skimming your code, I see a problem with your loop. You're trying to make it fire ever 0 seconds which is impossible. The shortest wait you can do is wait(), you must put a number inside of your wait or put nothing inside. Also, this loop appears to never end. You may want to add and if condition inside that will lead to a break

Okay, Simple fix

local LocalPlayer = game:GetService("Players").LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait()

LocalPlayer.PlayerModel.PetEquipped.Changed:connect(function()
 if LocalPlayer.PlayerModel.PetEquipped.Value == "Dog" then
Pet = game.ReplicatedStorage.Dog:Clone()
Pet.Parent = Character
Pet.Name = "Pet"
Pet.CanCollide = false
Pet.Transparency = 0.25 -- set transparency
Pet.Size = Vector3.new(2, 2, 2) --set size here :P
BodyPosition = Instance.new("BodyPosition", Pet)


while true do
wait()
BodyPosition.Position = Character.Head.CFrame:pointToWorldSpace(Vector3.new(2, 1, 0))
end
end
end)

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