Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to call the character of player?

Asked by 15 days ago
Edited by Leamir 15 days ago
-- 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

Log in to vote
0
Answered by 15 days ago

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")
0
This will only work client-sided. raid6n 2135 — 15d
0
yes, his localscript is in a tool NathanBlox_Studios 100 — 15d
0
the problem is always the character. It fails to index it or (nil). hitmna123 19 — 15d
0
Maybe replace game:GetService("Players") with game.Player NathanBlox_Studios 100 — 15d
Ad
Log in to vote
0
Answered by 15 days ago
Edited 15 days ago

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. :)

0
oops, made a mistake. CosmiCreator 0 — 15d
0
there is always a problem for me idk why. hitmna123 19 — 15d

Answer this question