--[[
	Sets up the EmoteManager for the given player.
]]

local function setupEmoteManager(player)
	local humanoid = player.Character:WaitForChild("Humanoid")
	local humanoidDescription = humanoid:WaitForChild("HumanoidDescription")
	emoteManager:setup(player, humanoidDescription)

	local connection
	connection = humanoid.Died:Connect(function()
		stopEmote(player)
		player.CharacterAdded:Wait()
		setupEmoteManager(player)
		connection:Disconnect()
	end)
end

local function onPlayerAdded(player)
	if not player.Character then
		player.CharacterAdded:Wait()
	end
	loadEmoteAssets()
	setupEmoteManager(player)
end

local function onPlayerRemoving(player)
	stopEmote(player)

	emoteManager:cleanup(player.UserId)
end

local function onChangeEmoteLock(player, emoteName, isLocked)
	assert(onChangeEmoteLockCheck(emoteName, isLocked))

	emoteManager:setEmoteIsLockedForPlayer(player, emoteName, isLocked)
end

emotePlayed.OnServerEvent:Connect(startEmote)
emoteStopped.OnServerEvent:Connect(stopEmote)
changeEmoteLock.OnServerEvent:Connect(onChangeEmoteLock)

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

loadEmoteAssets()
