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 8 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?

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

Log in to vote
0
Answered by 8 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 8 years ago

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)

Answer this question