Hello, I,m making game that makes other player flying around, but something is happen when I resetting the character. It will showing the error If try to reset it.
Here’s the code:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://4234108826"
local PlayAnim = Humanoid:LoadAnimation(Anim)
local HumaoidRP = Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
As you can see, I wait for the humanoid to exist, I still get the error. It only seems to happen in Studio, but it’s really hindering when you want to test.
This is because the player’s character is not immediately parented to the workspace. You need to wait until the character is in the workspace using something like AncestryChanged or ChildAdded.
That won’t work as Character is a property of Player, not an object under Player.
You would have to use something like this to achieve the same functionality
repeat wait() until Player.Character
local Character = Player.Character
Also, if you are getting this error whenever you reset, it might be that the humanoid’s parent is set to nil. If that’s the case, this is perfectly normal.
Instead of a repeat loop, just use local character = player.Character or player.CharacterAdded:Wait(). Even if the Character doesn’t exist, it’ll just wait until it does.
As another person here said, it would be better to use local char = player.Character or player.CharacterAdded:Wait(), because it will wait until the character loads in, and will continue the script once the character loads in