So I have a client script that tweens a swinging axe back and fourth and everything was working until it popped up with this warning: Infinite yield possible on ‘Workspace.Stages.12.Axes.AxeSwing.Axe:WaitForChild(“HingePart”)’
HingePart was there but it still warned.
Code:
local TweenService = game:GetService("TweenService")
local model = script.Parent
local HingePart = model:WaitForChild("HingePart") <-----------------
local HingeConstraint = HingePart:WaitForChild("HingeConstraint")
local function TweenAxe()
local tweenInfo = TweenInfo.new(0.8, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
local tween = TweenService:Create(HingeConstraint, tweenInfo, {TargetAngle = 80})
tween:Play()
tween.Completed:Connect(function()
local tweenInfo = TweenInfo.new(0.8, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
local tween = TweenService:Create(HingeConstraint, tweenInfo, {TargetAngle = -80})
tween:Play()
tween.Completed:Connect(function()
TweenAxe()
end)
end)
end
TweenAxe()
Is is not an error, it is just a warning to tell you that the script will infinitely wait until the object is found.
You can add a small additional check and a wait time to avoid it.
local model = script.Parent
local HingePart = model and model:WaitForChild("HingePart", 30)
local HingeConstraint = HingePart and HingePart:WaitForChild("HingeConstraint", 30)
“Thing and Thing…” in the variable is a check to make sure the object is found before starting to search the next one, it is same as “if Thing then”.
Objects are not removed from the workspace when StreamingEnabled is activated or when graphics quality is low, they are just visually hidden… so it can’t break scripts in any way and it is not the cause of this warning.
Also the script is inside the model, so even if it get moved it doesn’t do anything to the script.
I’m not sure why it doesn’t rotate, but at first i see that you have 2 different Tween and TweenInfos variable with the same name, so the script can be confused on which one he should use.
Both tween infos are exactly same, which mean one useless and only one is enough.
As your not playing tweens at same time, only one tween variable is needed, so you just need to update it.
Also putting function inside function isn’t the best move in programming, you should do something like this instead:
local TweenService = game:GetService("TweenService")
local Model = script.Parent
local HingePart = Model and Model:WaitForChild("HingePart", 30)
local HingeConstraint = HingePart and HingePart:WaitForChild("HingeConstraint", 30)
local TweenInfos = TweenInfo.new(0.8, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
local Tween = nil
local State = true
local function TweenAxe()
if State == true then
State = false
Tween = TweenService:Create(HingeConstraint, TweenInfos, {TargetAngle = 80})
Tween:Play()
elseif State == false then
State = true
Tween = TweenService:Create(HingeConstraint, TweenInfos, {TargetAngle = -80})
Tween:Play()
end
end
Tween.Completed:Connect(TweenAxe)
TweenAxe()
This is called recursion, and it absolutely has its use cases. The code in question might be a poor example since it can easily be replicated in another way, but there are many other use cases where recursion is extremely helpful. Calling it “not the best move” is a bit of a stretch.
There is a huge difference between function recursion and multiple functions inside a function… recursion is usefull while functions inside a function is useless, messy, disorganized and make code more difficult to read.
functions recursion is this:
local function recursion()
--Code
recursion()
end)
What i’m talking about is this:
local function Thing1()
--code
local function Thing2()
--code
local function Thing3()
--code
end)
end)
end)
If you don’t know, please don’t make misleading statements like that. Especially if the docs refute your claims - unless you’re 1000% sure you’re correct.
Alright i’m sorry then, i was just in doubt because like i said, i didn’t saw the parts being removed from workspace, and scripts didn’t printed anything even when streamingEnabled was activated and parts visually hidden.
Wait what, local scripts aren’t supposed to be able to run in the workspace…
If you want to do it in client side you need put the local script into StarterPlayerScript and do everything from there.
I remember creating a local script by changing the context to a local script instead of creating one. Apparently, this will run your script twice, which might be causing issues for you, so either use a normal script in workspace, like @Crygen54 mentioned, or create a new local script and paste it in there