Player.WaitForChild("Character") obv doesn't work, but what could be used instead of it ?
Asked by
5 years ago Edited 5 years ago
My code works fine... its this one:
1 | script.Parent.Touched:connect( function (hit) |
3 | for i,v in pairs (game.Players:GetPlayers()) do |
4 | v.Character.UpperTorso.Transparency = v.Character.UpperTorso.Transparency + 0.1 |
But I also get the following error :
attempt to index field 'Character' (a nil value)
How do I solve it ? Why is v.Character a nil value ? Doesn't the GetPlayers function return only players as v values? I think every player should have a character... is it maybe that some characters arent loaded yet when the function is fired for the first time ? Because this part is not anchored and the baseplate it stays on is anchored...
Still.. I can't do v.WaitForChild("Character") , is there any other ways for me to prevent getting this error ? It is a harmless one but I'd love to get completely rid of it.
Edit1 : I don't want to check if the toucher has a Humanoid or not.. Cuz There could be a person touching this brick when someone else was newly joining and I'd get an error cuz the new players character wouldn't have been loaded...
Edit2 : The right answer came fast, and it was clear, but I want to add something for people wandering around to also understand.. The correct solution is
01 | script.Parent.Touched:connect( function (hit) |
03 | for i,v in pairs (game.Players:GetPlayers()) do |
04 | local charac = v.Character or v.CharacterAdded:wait() |
05 | charac.UpperTorso.Transparency = charac.UpperTorso.Transparency + 0.1 |
but the important part is that as the answerer mentioned, it jumps to the second argument of OR when v.Character gives nil...
When you do this;
local charac = v.CharacterAdded:wait() or v.Character
as in.. Opposite order
The script wont work correct when you have loaded characters, instead, it will only work when there are some unloaded characters...