Infinite yield possible error for gravity coil

Hello fellow developers!

I am developing a gravity coil and wrote the script as follows:

You can see “Infinite yield possible error” for line 19.

I searched for hours and some says this is no problem, but actually I found the behavior is unstable.

When I unequip the tool, I still can jump higher for a while and even keep lifting to the upper limit sometimes.

I don’t now how to fix the problem and so appreciate if someone can help.

Is the name of your character “Character”? If not, then you would have to wait for your name.
However it is easier to do this:

local char = game.Players.LocalPlayer.Character

It’s caused by too many .Parent statements. Remove one

Also consider setting variables such as Player and Character at the start of your script to make them more easily understood. if you added the following to the start of the script:

local Tool = script.Parent
local character = Tool.Parent 

Your script can then be simplified. For example:

-- Easier to read what it relates to
Tool.Equipped:Connect(function()
1 Like

Thank you for your reply.
I use a server script, and so I think I cannot use “LocalPlayer”.

Well yea, you get the infinite yield because there’s no such instance, ‘Character’. When a tool is unequipped, it gets parented to the backpack. I do recommended the above method, but if you still want to fetch the character in the scope you could do:

local player = tool.Parent:FindFirstAncestorOfClass('Player')
local char = player.Character or player.CharacterAdded:Wait()
1 Like

Then use

local char = script.Parent.Parent.Parent.Character
1 Like

I tried this and found it work. Thank you so much!

1 Like

Thank you for your advice.
I rewrote the script and found the script simpler and easy to see!

I got this script from YouTube and didn’t think well.
Now I understand why the behavior became unstable.
Also thank you for showing me another way to fetch the character!

1 Like