Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

While true do loop in a script?

Asked by 9 years ago

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?

01local LocalPlayer = game:GetService("Players").LocalPlayer
02local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait()
03 
04LocalPlayer.PlayerModel.PetEquipped.Changed:connect(function()
05 if LocalPlayer.PlayerModel.PetEquipped.Value == "Dog" then
06Pet = game.ReplicatedStorage.Dog:Clone()
07Pet.Parent = Character
08Pet.Name = "Pet"
09Pet.CanCollide = false
10Pet.Transparency = 0.25 -- set transparency
11Pet.Size = Vector3.new(2, 2, 2) --set size here :P
12BodyPosition = Instance.new("BodyPosition", Pet)
13 
14 
15while wait(0) do
16    BodyPosition.Position = Character.Head.CFrame:pointToWorldSpace(Vector3.new(2, 1, 0))
17end
18end
19end)

Thanks!

2 answers

Log in to vote
0
Answered by 9 years ago

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

Ad
Log in to vote
0
Answered by 9 years ago

Okay, Simple fix

01local LocalPlayer = game:GetService("Players").LocalPlayer
02local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait()
03 
04LocalPlayer.PlayerModel.PetEquipped.Changed:connect(function()
05 if LocalPlayer.PlayerModel.PetEquipped.Value == "Dog" then
06Pet = game.ReplicatedStorage.Dog:Clone()
07Pet.Parent = Character
08Pet.Name = "Pet"
09Pet.CanCollide = false
10Pet.Transparency = 0.25 -- set transparency
11Pet.Size = Vector3.new(2, 2, 2) --set size here :P
12BodyPosition = Instance.new("BodyPosition", Pet)
13 
14 
15while true do
16wait()
17BodyPosition.Position = Character.Head.CFrame:pointToWorldSpace(Vector3.new(2, 1, 0))
18end
19end
20end)

Answer this question