Help with getting players character

I have a tool that plays a animation on use but im having trouble getting the players character so it can play a animation.

tool = script.Parent

local cooldown = false
cooldown = false
local Player = game.Players.LocalPlayer
tool.Activated:Connect(function()
	local character = Player.Character or Player.CharacterAdded:Wait()
	if cooldown == false then
		cooldown = true
		local ID = "http://www.roblox.com/asset/?id=8208475398"
		local animation = Instance.new("Animation")
		animation.AnimationId = ID
wait (3)

		character.Humanoid:LoadAnimation(animation):Play()
		game.Workspace["slenderW0lf_YT"].Pants.PantsTemplate = "http://www.roblox.com/asset/?id=PLACEHOLDER"
		--hi
		game.Workspace["slenderW0lf_YT"].Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=PLACEHOLDER"
		print "yets"
		wait (10)
		cooldown = false

	end
end)

Is this a regular Script, or a LocalScript?

it is a script
aaaaaaaaaaaaaaaaa

You cannot access LocalPlayer through a script.
Therefore you also won’t be able to get the character.
Use a local script instead.

Are there any errors in the output?

It was a error about trying to get the character through a script but it worked when I put it in a local script.

Ah

Since this is just a regular Script, the Player you’re attempting to index would actually be equal to nil because only LocalScripts are capable of finding the “Local” Player

But we do have our workarounds on finding the Character through a Script, so don’t worry

By default when you activate a Tool using that Event, you can reference the Character by using Tool.Parent as shown in the hierarchy, the order goes from Script > Tool > Character > workspace so we just want the Character for now

Try this and see if it works

local tool = script.Parent
local cooldown = false

tool.Activated:Connect(function()
	local character = tool.Parent
    local Humanoid = character:WaitForChild("Humanoid")

	if cooldown == false then
		cooldown = true
		local ID = "http://www.roblox.com/asset/?id=8208475398"
		local animation = Instance.new("Animation")
		animation.AnimationId = ID
        wait(3)
		Humanoid.Animator:LoadAnimation(animation):Play()
		Character.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=PLACEHOLDER"
		--hi
		Character.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=PLACEHOLDER"
		print "yets"
		wait(10)
		cooldown = false
	end
end)
3 Likes

That’s because you can only access LocalPlayer through a LocalScript. You cannot reference a LocalPlayer through a normal script!

1 Like
tool = script.Parent
local cooldown = false
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://8208475398"
animation.Parent = script

tool.Activated:Connect(function()
	if cooldown then
		return
	end
	local character = tool.Parent
	if character then
		cooldown = true
		local humanoid = character:WaitForChild("Humanoid")
		local anim = humanoid:LoadAnimation(animation)
		repeat
			task.wait()
		until anim.Length > 0
		anim:Play()
		if character.Name == "slenderW0lf_YT" then
			character.Pants.PantsTemplate = "rbxassetid://0" --change 0
			character.Shirt.ShirtTemplate = "rbxassetid://0" --change 0
		end
		task.wait(10)
		cooldown = false
	end
end)

Optimised the script and made sure the created Animation instance is parented (otherwise its parent is nil), you also don’t need to create a new animation each time the “Activated” event is fired, just create it once at the top of the script and load/play it each time the event is fired instead.

1 Like