Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to call the character of player?

-- Tools
local tool = script.Parent
local handle = tool:WaitForChild("Handle")

-- Player
local players = game:GetService("Players")
local player =  players.LocalPlayer
local char = player.Character
if not char then
	char = player.CharacterAdded:Wait()
end
local humanoid = character:FindFirstChild("Humanoid")


-- Animation
local animation = Instance.new("Animation")
animation.AnimationId = "https://www.roblox.com/library/5446663397/swordequip"
local animationtrack = humanoid:LoadAnimation(animation)


tool.Equipped:Connect(function()
	print("tool equipped")
	animationtrack:Play()
end)

My script is not working because local char = player.Character is throwing an error, that it can't index the character

2 answers

Instead of putting it in an if statement, try connecting both with and or.

Like this:

local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")

4 comments

0

This will only work client-sided.

0

yes, his localscript is in a tool

0

the problem is always the character. It fails to index it or (nil).

0

Maybe replace game:GetService("Players") with game.Player

You can use GetPlayerFromCharacter to get the character of the Local Player.

local plr = game.Players.LocalPlayer
local Player = game.Workspace[plr.Name]
local char = game.Players:GetPlayerFromCharacter(Player)

This works for me everytime. :)

2 comments

0

oops, made a mistake.

0

there is always a problem for me idk why.

This discussion is preserved as it was. New answers, comments, and votes are closed.