Goal
So I want to understand how to access the Player, Character & Humanoid in studio, I have searched up tutorials on how to do it, but it never really works and they don’t explain why it happens, I have always had trouble with scenarios like this.
The Problem
The error is on Line no. 5 it gives me the error *Workspace.Part.Script:5: attempt to index nil with ‘Character’" any explanations would be helpful.
local Part = script.Parent
wait(4)
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Player.Character:WaitForChild("Humanoid")
The error could indicate that you’re trying to get LocalPlayer from a server script, it only works in local scripts, what are you trying to get the Humanoid for exactly? The way to get it depends on your use case
local getplayer = "" -- put the player name
local Part = script.Parent
wait(4)
local Players = game:GetService("Players")
local Player = Players[getplayer]
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Player.Character:WaitForChild("Humanoid")
If the script is in StarterCharacterScripts then you can just use:
local Humanoid = script.Parent:WaitForChild("Humanoid",3);
If it is in StarterPlayerScripts then use this:
local LocalPlayer = game:GetService("Players").LocalPlayer;
local Character = LocalPlayer.Character
if not Character or not Character.Parent then
Character = LocalPlayer.CharacterAdded:wait();
end
local Humanoid = Character:WaitForChild("Humanoid",3);
If the script is a Script (i.e. runs on the server) then use this:
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid",3);
end)
end)
alr man @EmbatTheHybrid alr said this but local player is ONLY for local scripts nice way to remember is that they both have the word local in it but if you wanna get the plr or character from studio you gotta know which one for example you wanna get the player FriendlyFrenchFry123 from studio then just do
local plrs = game:getservice("players")
local someplr = plrs:GetPlayerFromCharacter(workspace.FriendlyFrenchFry123)
oh yeah btw it says nil bc player is nil the variable player is nil