Infinite Yield Possible

I got a Inf Yield error from the torso vairble:

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Torso = Character:WaitForChild("Torso") or Character:FindFirstChild("UpperTorso")

I made sure the avatar settings for the game is R6.

Let me know if you need more script information.

Fairly certain you’d have to make a custom function for that, haven’t tested it but it’s pretty self-explanatory:

local function waitForFirstChild(parent, name1, name2)
    local initialTick, child = tick(), nil
    repeat
        child = parent:FindFirstChild(name1) or parent:FindFirstChild(name2)
        if tick() - initialTick > 5 and not child then
            warn('Infinite yield possible on',parent:GetFullName())
        end
        if not child then
            task.wait()
        end
    until child
    return child
end

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Torso = waitForFirstChild(Character, 'Torso', 'UpperTorso')

Gave me several warnings.

image

I see, it’s because you’re referencing an old character.

Can you show your full script?

The script is too big to paste into here. So I’ll just send a model link for it.

I see, I think it had to do with the scope of your functions. Try putting everything that doesn’t belong to a function inside of your tool.Equipped function:

Tool.Equipped:connect(function(TempMouse)
	local Character = script.Parent
	local Humanoid = Character:WaitForChild("Humanoid")
	local Torso = waitForFirstChild(Character, 'Torso', 'UpperTorso')
-- etc
local players = game:GetService('Players')
local player = players.LocalPlayer

local character
local humanoid
local torso

player.CharacterAdded:Connect(function()
	character = player.Character
	humanoid = character:WaitForChild('Humanoid')
	if humanoid ~= nil then
		if humanoid.RigType == Enum.HumanoidRigType.R6 then
			torso = character:WaitForChild('Torso')
		elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
			torso = character:WaitForChild('UpperTorso')
		end
	end
end)

Haven’t tested but it should work

I haven’t seen the script that you sent, I am just basing this off of the first script from the initial post

1 Like

image

There’s over 66 functions and I’m not sure I can do that in the amount of time I have.

How long is the script? If you could, could you send it here in text form?

Please also make sure that the variable names match!

I tried to send the script here but it’s too long so I made a model. It’s a few replies above.

Alright I will check it out. Give me a moment.

GunScript_Local.lua (95.0 KB)

Alright, check this out. Also, I suggest that you try to organize your code, especially variables.

The code was from a free model so I didn’t really mess with it.

I’m not getting an Infinite Yield anymore and I can fix the rest of the errors so thanks. :+1: