How would replace wait(n) in this kind of scripting situation?

Hello everyone, I recently discovered that wait() or wait(n) are not the best choices at all when in need to wait a specific amount of time. Of course, there are ways to fix this problem by using tick() or os.time(), etc… but I don’t know how you would implement it in this example script below:

-- Example script
while true do
  wait() -- or wait(n)
  PetObj.Name.Value = PetObj:WaitForChild("Obj").ObjName.Value
end

You should not be using while loops for this. Your scripts should be primarily events-based. You want to change it to something along these lines.

PetObj:WaitForChild("Obj").ObjName.Changed:Connect(function(newValue)
    PetObj.Name.Value = newValue
end)

If this worked for you, be sure to mark it as the solution for others to see it was solved. Also, you might find these links useful if you’re interested in learning more.

1 Like

@snowstormsindecember I did not read every useful links(btw thx so much for those they learned me new ways of scripting), but what if I need to set a value and then verify the value?

Example:

while true do
	wait()
	PetObj.PetName.Value = PetObj:WaitForChild("Obj").ObjName.Value
end

PetName.Changed:Connect(function()
	if PetName.Value == "Egg" then
		PetObj.Animator.IdleAnim.AnimationId = "rbxassetid://5944474604"
		PetObj.Animator.WalkAnim.AnimationId = "rbxassetid://5944470999"
	elseif PetName.Value == "Giraffe" then
		PetObj.Animator.IdleAnim.AnimationId = "rbxassetid://5951899040"
		PetObj.Animator.WalkAnim.AnimationId = ""
	end
end)

like how would I set a value(PetName.Value = ObjName.Value) multiple times(like a loop) and then verify that value(in this case PetName)