How to get the character because player.Character isn't working

Hi, I tried writing this before but I didn’t get a response I tried player.Character but when trying to get the humanoid it said that character was nil and I’ve tried player.CharacterAdded:Wait() ,but the character changes at the start of my game when the player clicks on a gui button so I don’t know how to get the character if anyone helps, thanks so much.

1 Like
game:GetService("Players").PlayerAdded:Connect(function(player)
       player.CharacterAdded:Connect(function(character)

character:WaitForChild("Humanoid")

       end)
end)
1 Like

Sorry, I should have specified my real question is how to get the character not how to get the humanoid ,but thanks for the response

game:GetService("Players").PlayerAdded:Connect(function(player)
       player.CharacterAdded:Connect(function(character)

-- U can use now `character` for something

       end)
end)

I just meant what would I write for local character = because I don’t know how to use the function in this situation

u have the character there u can do with character something u want could u explain better what u want to do

Bruh. Simple. It’s just 2 lines of code.

--//Local script
local player = game.Players.LocalPlayer
local character = game.Workspace:WaitForChild(player.Name)

Maybe this will work?

U dont need workspace u can just do local character = player.Character

That wont work like he mentioned

It should work but i dont know

Try it out

--//Local script
local player = game.Players.LocalPlayer
local character = game.Workspace:WaitForChild(player.Name)
print("Got Player")

You can do so much things with it

Do this instead. I think OP may just not be using the properties correctly.

local player = game:GetService("Players").LocalPlayer
local character = (player.Character or player.CharacterAdded:Wait())
print("Got Player")

I’m trying it I don’t get why any of this isn’t working its so weird I’m gonna try it on different scripts and see now

Ok I’ll try it right now and see if it works

Could you send the full script? That would definitely help.

I was testing everyone’s code in a shield tool I have but its not equipping after I switch character using a button I created

local player = game.Players.LocalPlayer
--local character = player.Character or player.CharacterAdded:Wait()
local character = game.Workspace:WaitForChild(player.Name)

local shield = script.Parent

local Backpack = player.Backpack


local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")


local AnimationTrack = Instance.new("Animation")
AnimationTrack.AnimationId = "rbxassetid://10780183458"
local AnimationTrack = Animator:LoadAnimation(AnimationTrack)
AnimationTrack.Priority = Enum.AnimationPriority.Action
AnimationTrack.Looped = true

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local desiredKey = Enum.KeyCode.Q

local function keyIsDown()
	return UserInputService:IsKeyDown(desiredKey)
end

local tool = player.Backpack:WaitForChild("Shield") 
local handle = tool.Handle

local isHeld = false;

local function removedebounce()
	if not UserInputService:IsKeyDown(desiredKey) then
		isHeld = false;
	end
end
	
	

local function equipTool()
	if UserInputService:IsKeyDown(desiredKey) then
		if isHeld == false then
			humanoid:EquipTool(tool)
			AnimationTrack:Play()
			humanoid.WalkSpeed = 0
			isHeld = true
		else if isHeld == true then 
				return
			end

		end 
	else tool.Parent = Backpack
		AnimationTrack:Stop()
		humanoid.WalkSpeed = 16
	end
end

RunService.Heartbeat:Connect(equipTool)
RunService.Heartbeat:Connect(removedebounce)


Whoa whoa whoa WHOA. Clean up your code there buddy. Scripts can break your code sometimes.

Wait I think I know the problem.
How do you switch characters, like in a script?

I think he means Local or Server-Sided.