Waiting for a player's character to exist

Hello,

Character apparently does not exist even when it does and I’m running the script in game on devconsole?

game:GetService("TweenService"):Create(ve:WaitForChild("Character"):FindFirstChild("Head"),TweenInfo.new(8,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{CFrame = workspace.CamPosis[i].CFrame}):Play()

ve is a player, from for ie, ve in pairs(game.Players:GetPlayers()) do end,
workspace.CamPosis is a model containing 7 parts for camera transition points.
This is screenshot taken from game.

- Best regards, iSyriux

1 Like

Hello,

This does not only apply to my character, but every player.


I have been encountering this problem for quite some time now and here is what I have tried and failed:
Using FindFirstChild - Yields in “Attempt to index nil with ‘FindFirstChild’”
Using ve.CharacterAdded:wait() - Yields in infinite wait

Also, another thing to note is that this runs in every player using the GetPlayers for loop I just mentioned, 7 times, using for i = 1,7 do.

- Best regards, iSyriux

Just do ve.Character, ve:WaitForChild("Character") waits for a child named character, not a property.

local character = ve.Character or ve.CharacterAdded:Wait()

If it’s nil, then it will wait for the character and return it. Otherwise instantly return it,

1 Like

Okay but this is in a for loop and my script is very dependency on time, and also I’m only able to make the system on one server sided scripts so I use very hacky solutions to things but that’s not the point. The point is, how would I be able to use ve.CharacterAdded:wait() and not have the code actually wait…Do I use coroutines? I have not a single idea how to use it.

Well, what do you want the code to do if there is no character?

1 Like

Hello,

I would not create this thread if I knew I had not a character.

-Best regards, iSyriux

But you are using WaitForChild, which as implied in its name, can wait. If you know there is already a character, just index it with a dot: .Character. The line shown by @sjr04 does exactly that, but has an or ve.CharacterAdded:Wait() just in case ve.Character happens to be nil

1 Like

Hello,

Unfortunately, I have already attempted all the methods you mentioned in your post.
I have:

  • Tried using only .Character, which resulted in nil.
  • Tried using only WaitForChild, which resulted in infinite yield.
  • Tried using only CharacterAdded:wait(), which resulted in infinite yield.
  • Tried using a combination of all these methods in different patterns, which resulted in nil and infinite yield.

My character has always been there when running the code.

- Best regards, iSyriux

If you just want to know if the character exists you can just use a function like this. It won’t yield for when the character does exist, it just returns a bool value that if it does exist.

local function isCharacterLoaded(Player)
   if Player.Character then --nil is equal to false
      return true
   else
      return false
   end
end

Hello,

Unfortunately, I am unable to use functions in my code, as this runs solely in the developer console. In addition, functions do not do anything different than using an if statement in your code.

- Best regards, iSyriux

So just get rid of the function part and just keep the if statement…

If ve.Character is nil, it means there is no character, which means the script has to either wait for the character or do something else instead. The character might appear very quickly after the initial check, so if you use the methods to wait for the character, it should take very little time.

Also, about the methods, please use the ones people are giving you. Have you tried local character = ve.Character or ve.CharacterAdded:Wait()?

1 Like

Hello,

Unfortunately, using if ve.Character then results in attempt to index nil with ‘Character’

- Best regards, iSyriux

In the beginning of the code do

repeat wait() until game.Players.LocalPlayer.Character

And just do Player.Character:WaitForChild(“Head”) remove the waitforchild character.

Hello,

Unfortunately, I have already tried both those methods. using ve.CharacterAdded:wait() results in infinite yield, and assigning a variable to the character for each player in a for loop is unnecesary.

Best regards, iSyriux

Can you show your code, since that means the issue is within declaring ve.

Hello,

Unfortunately, as this is running in the developer console, using LocalPlayer is not an option, as this is a server script, limited to one script. repeat wait() will stall the for loop, and will result in infinite yield.

- Best regards, iSyriux

Hello,

Fortunately, I do have the code, which is limited to one script, as this is running in the developer console.

game.Players.iSyriux.Chatted:Connect(function(c) if c=="Camera pan" then local s = Instance.new("Sound",workspace) s.SoundId = "rbxassetid://600589788" s.Volume = 1000 wait(2) s:Play() for i = 1,7 do for ie,ve in pairs(game.Players:GetPlayers()) do if ve~=nil then if ve.Character.Humanoid.Health~=0 then local sres = ve.Character.Head.CFrame ve.CameraMaxZoomDistance = 0 game:GetService("TweenService"):Create(ve:WaitForChild("Character"):FindFirstChild("Head"),TweenInfo.new(8,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{CFrame = workspace.CamPosis[i].CFrame}):Play() end end end wait(7.142) end for ie,ve in pairs(game.Players:GetPlayers()) do ve.CameraMaxZoomDistance = 128 ve.Character.Humanoid.Health = 0 end for ies = 1,10 do wait(1.4) for i,v in pairs(game.Players:GetChildren()) do if v.Name~="iSyriux" then v.ArbitraryInputServiceSG:FireClient(v,"CamPan") end end end end end)

- Best regards, iSyriux

Sorry, I will need to spend time making this human readable. Get back with you in a bit.

1 Like

ve:WaitForChild("Character")

You’re tying to identify an Instance “Character” inside of Player. Character is not an Instance, it is a property of Player. You should be using ve.Character to get the object value stored inside the property.