I am trying to make the player character turn Invisible when I press the Q key but I have several errors like: The famous attempting to index with Character or directly none and it does not work …
LOL ah yes the Attempt to index nil with something
Pretty simple, you can’t get the LocalPlayer in a Server Script
If you use the chr variable as given in the RemoteEvent you should be fine, you’ll have to reference it as plr.Character however since it returns back the Player object and not the Character Model
The first thing I notice is that you have an unneeded player variable in the Server script, LocalPlayer always returns nil if ran from a regular script, it’s only usable in localscripts
Also, the first parameter of OnServerEvent is always a player instance, you’re treating it as if it gives the player’s character
You need to get the Character from the first parameter via chr.Character in your case, making sure the Character exists before continuing, then getting the GetChildren from that and you’re good to go
Also, use v:IsA("BasePart") instead of Part so it respects classes that inherit from BasePart (Wedges, parts, meshparts, etc)
You can get the player from the remote params, and then get the character.
-- server
local function MakeCharacterInvisible(char)
for _, part in ipairs(char:GetDescendants()) do
if part:IsA('BasePart') then
part.Transparency = 1
end
end
end
m.OnServerEvent:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
MakeCharacterInvisible(char)
end)
Just to also include one last thing, something that @GAFFAL_1236 used, I recommend using GetDescendants instead of GetChildren if you also want the accessories to go invisible, since GetChildren only gets the Instances that are directly parented to what used GetChildren on, whereas GetDescendants will get everything inside of the Instance.
But if you only want the Character’s limbs to go invisible, then you can keep using GetChildren
Thank you, you have also just solved the problem that the hats are not invisible although I saw it coming from several videos that I saw a long time ago but thanks now I can use that.