Chat event doesn't work

I’m trying to start an animation when saying a certain word/sentence

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local anim = character:WaitForChild(“Humanoid”):LoadAnimation(animation)
player.Chatted:Connect(function(message)
if message == “test” then
anim:Play(5718117993)
end
end)
end)
end)

I’m new to scripting so I don’t really know what to do, I checked dev console for errors as well

1 Like

You never defined animation.

animation will be an Animation instance. You don’t pass the AnimationId to :Play btw. You define it in the Animation instance.

Also you do not want to put the Player.Chatted inside of the CharacterAdded event because it will create a new one everytime you respawn.

local animation = Instance.new("Animation")

animation.AnimationId = "rbxassetid://5718117993"

game.Players.PlayerAdded:Connect(function(player)
	local track
	
	player.CharacterAdded:Connect(function(character)
		character.AncestryChanged:Wait() --// it is required the Character be a descendant of game before loading an animation into it. For a short period the character exists but has no parent, so we wait until its ancestry changes (which means it usually was parented under game; i dont think it ever gets parented to something else besides game while its parent is initially nil)
		track = character:WaitForChild("Humanoid"):LoadAnimation(animation)
	end)
	
	player.Chatted:Connect(function(message)
		if (message == "test") then
			if (track) then --// make sure track exists
				track:Play()
			end
		end
	end)
end)
1 Like