Sorry for the bad English, I'm not native.
I was trying to do a dash script for my game,the code was working well, but I wanted that the dash would only be possible while jumping.So, i tried to put a humanoid:GetState() == Enum.HumanoidStateType.Freefall,and didn't work.So,I tried putting local character= script.Parent and local humanoid= character:WaitForChild("Humanoid"), but now when I try to run the code, Infinite yield possible on'Players.extroias.PlayerScripts:WaitForChild("Humanoid")' appears on the output.
here's the code
local UIS = game:GetService("UserInputService") local localPlayer = game.Players.LocalPlayer local character= script.Parent local humanoid= character:WaitForChild("Humanoid") local CD=5 UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.E and humanoid:GetState() == Enum.HumanoidStateType.Freefall then local Root = localPlayer.character:WaitForChild("HumanoidRootPart") Root.Velocity = Root.Velocity + Root.CFrame.lookVector*90 script.Disabled = true wait(CD) script.Disabled = false end end)
I want to know what does this output mean, and how to fix it.
:WaitForChild() acts the same as :FindFirstChild(), except it yields the whole thread until the requested child is found, immediately returning the instance afterwards.
When the warning appears, it tries to warn you that the Instance you're trying to get might not exist. That being said, unless the child is found, the function will probably yield infinitely, causing a permanent delay of the execution, since there is no specified timeout for the function.
As far as I know, here is the method that I used to prevent the message from displaying:
There's an optional timeout parameter for the function, which indicates seconds until when the function should stop yielding, therefore continuing the thread:
local thing = Instance:WaitForChild("name", 10) -- this will pause the current thread until when "name" is found or after 10 seconds is reached --If the timeout is reached, thing = nil
I'd recommend 30 seconds for the timeout.
Though, I have to say. It's better to not have the timeout if you don't want the function to return nil if the instance is not found on time. If the script runs before the character is loaded, I'd suggest not adding the timeout parameter, but rather execute the script when the character is fully loaded. You may want to consider wrapping your function to a pcall() if you don't prefer the two afore-mentioned suggestions.
As I can see, you put this script in StarterPlayerScripts, correct? I'd suggest putting it in StarterCharacterScripts, so the script will be replicated to the character instead of the PlayerScripts folder. That is a part of the reasons why the warning displayed. (Shawnyg also pointed out that)
Also, I kinda edited your script a bit, maybe you wanna take a look? Glad to help c:
--[[ Put into StarterCharacterScripts Not StarterPlayerScripts since you attempted to search Humanoid in PlayerScripts folder You can use Shawnyg's method, too! But you'll have to check whether if the character is available, or else the script will error. Putting this into StarterCharacterScripts is the efficient method since all you have to do here is to manipulate character's movements ]] local uis = game:GetService("UserInputService") local character = game.Players.LocalPlayer.Character local humanoid = script.Parent:WaitForChild("Humanoid") -- I don't want this one to be a nil value so no timeout. Don't worry, the script will continue to run once the Humanoid is found. local hrp = script.Parent:WaitForChild("HumanoidRootPart") -- same as above local debounce = false -- instead of enabling and disabling the script for the cooldown local COOLDOWN = 5 --seconds local VELOCITY = 90 uis.InputBegan:Connect(function(input) if input .KeyCode == Enum.KeyCode.E and humanoid:GetState() == Enum.HumanoidStateType.Freefall and not debounce then debounce = true hrp.Velocity = hrp.Velocity+hrp.CFrame.LookVector*VELOCITY wait(COOLDOWN) debounce = false end end)
Tell me if there's something wrong. Sorry for my English also!
This script is located in PlayerScripts, and will always remain in that folder. On line 3, you defined character
as script.Parent
, which would be the folder. On line 4 you look for a humanoid, which wouldn't be in the folder, but in the player's proper character. Reading over this script, I'd suggest changing line 3 to local character = localPlayer.Character
and putting this script into StarterPlayerScripts
it means it can't find the Humanoid so it might wait forever also the character variable is PlayerScripts so i changed it so its the actual character
local UIS = game:GetService("UserInputService") local localPlayer = game.Players.LocalPlayer repeat wait() until localPlayer.Character ~= nil local character= localPlayer.Character local humanoid= character:WaitForChild("Humanoid") local CD=5 UIS.InputBegan:Connect(function(input,bruh) if bruh == false and input.KeyCode == Enum.KeyCode.E and humanoid:GetState() == Enum.HumanoidStateType.Freefall then local Root = localPlayer.character:WaitForChild("HumanoidRootPart") Root.Velocity = Root.Velocity + Root.CFrame.lookVector*90 script.Disabled = true wait(CD) script.Disabled = false end end)