Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to call the character of player?

Asked by 3 years ago
Edited by Leamir 3 years 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 3 years 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 2196 — 3y
0
yes, his localscript is in a tool NathanBlox_Studios 212 — 3y
0
the problem is always the character. It fails to index it or (nil). hitmna123 21 — 3y
0
Maybe replace game:GetService("Players") with game.Player NathanBlox_Studios 212 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years 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 — 3y
0
there is always a problem for me idk why. hitmna123 21 — 3y

Answer this question