Best way to handle mood states

I’m trying to sus out the best way to handle player moods. At the moment for example, if a player sits at a seat, I check for a tag, and then adjust their Energy to go up every second they are sitting on said seat.

seat:Sit(Humanoid)

if seat:HasTag("MoodAdjustment") then -- Handle mood changes
	MoodService:StartAdjustment(player, seat:GetAttributes())

	local OccupantConnection
	OccupantConnection = seat:GetPropertyChangedSignal("Occupant"):Connect(function()
		OccupantConnection:Disconnect()

		MoodService:StopAdjustment(player)
	end)
end
-- Start giving mood adjustments while doing activities
function MoodService:StartAdjustment(player, moods)
	local PlayerCache = PlayerService.Cache[player]
	if not PlayerCache then
		return
	end

	local Data = DataService:Get(player)
	if not Data then
		return
	end

	if PlayerCache.DoingMoodAdjustment then
		return
	end

	PlayerCache.DoingMoodAdjustment = true

	task.spawn(function()
		while player and player.Parent and PlayerCache.DoingMoodAdjustment do
			for mood, amount in moods do
				Data.Moods[mood] += amount
			end

			task.wait(1)
		end
	end)
end

-- Stop giving any mood adjustments
function MoodService:StopAdjustment(player)
	local PlayerCache = PlayerService.Cache[player]
	if not PlayerCache then
		return
	end

	PlayerCache.DoingMoodAdjustment = false
end

Using attributes allows me to have certain seats or actions do more or less value, depending on what’s needed, however I am unsure if this code is really that “great”. Take into account everything here is a server sided, but I’m not sure how expandable this really is. For example, if we look at Bloxburg (which does this well), there’s hundreds of different tools/items/things to do that change all sorts of moods up and down, with I imagine some sort of server side authentication.

This current system also only really works on length based adjusters, like sitting/showering. But what if I want food to just be an instant increase with no while? I don’t want to have to code in specific use cases for every single item to do different things in each case.

Another issue is if player does multiple things at once, due to DoingMoodAdjustment variable, it blocks any stacking, which is good in the sense that I won’t get a ton of while loops going at once, but there may be certain use cases for a player to do one thing, while doing another, that needs to start and stop on command when needed

2 Likes