Repeat loop won't stop running after a player has stopped moving

Each set of movement from a player will increase one’s mood, meaning that if one gets to max fatigue bar for example (or fatigue = 100 or more) they would die.

Here is my my mood module.

function Moods.GetValue(MoodType,player)
	
	local folder = player:WaitForChild("MoodData")
	
	if MoodType == "Thirst" then
		local ValIns = folder:FindFirstChild("Thirst")
		return ValIns.Value
	elseif MoodType == "Hunger" then
		local ValIns = folder:FindFirstChild("Hunger")
		return ValIns.Value
	elseif MoodType == "Fatigue" then
		local ValIns = folder:FindFirstChild("Fatigue")
		return ValIns.Value
	elseif MoodType == "Dirtiness" then
		local ValIns = folder:FindFirstChild("Dirtiness")
		return ValIns.Value
	end
	
end

function Moods.DecreaseMood(MoodType,amount,player)

	local folder = player:WaitForChild("MoodData")

	if MoodType == "Thirst" then
		local ValIns = folder:FindFirstChild("Thirst")
		ValIns.Value = ValIns.Value + amount
	elseif MoodType == "Hunger" then
		local ValIns = folder:FindFirstChild("Hunger")
		ValIns.Value = ValIns.Value + amount
	elseif MoodType == "Fatigue" then
		local ValIns = folder:FindFirstChild("Fatigue")
		ValIns.Value = ValIns.Value + amount
	elseif MoodType == "Dirtiness" then
		local ValIns = folder:FindFirstChild("Dirtiness")
		ValIns.Value = ValIns.Value + amount
	end

end

Here is the updated Core Client

local pl = game.Players.LocalPlayer

local UtilModule = require(game.ReplicatedStorage.Modules.Util)
local Assets = require(game.ReplicatedStorage.Modules.Assets)
local MoodModule = require(game.ReplicatedStorage.Modules.Moods)
local permsmodule = require(game.ReplicatedStorage.Modules.Permissions)

local plChar = pl.Character

pl.CharacterAdded:Connect(function(char)
	local plhuman = char:WaitForChild("Humanoid")
	print(char.Name)
	print(plhuman.Name)
	plhuman:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
		print("walkspeed changed")
		if plhuman.WalkSpeed >= 21 then
			repeat
				MoodModule.DecreaseMood("Thirst",0.04,pl)
				MoodModule.DecreaseMood("Hunger",0.015,pl)
				MoodModule.DecreaseMood("Dirtiness",0.020,pl)
				MoodModule.DecreaseMood("Fatigue",0.04,pl)
				print("Removing mood for running")
				task.wait(1)
			until
			plhuman.WalkSpeed < 21
		elseif plhuman.WalkSpeed >= 16 and plhuman.WalkSpeed <= 18 then
			repeat
				MoodModule.DecreaseMood("Thirst",0.025,pl)
				MoodModule.DecreaseMood("Hunger",0.008,pl)
				MoodModule.DecreaseMood("Dirtiness",0.011,pl)
				MoodModule.DecreaseMood("Fatigue",0.03,pl)
				print("Removing mood for Walking")
				task.wait(1)
			until
			plhuman.WalkSpeed < 14
		end
	end)
	plhuman.Jumping:Connect(function(jumping)
		if jumping == true then
			repeat
				MoodModule.DecreaseMood("Thirst",0.3,pl)
				MoodModule.DecreaseMood("Hunger",0.015,pl)
				MoodModule.DecreaseMood("Dirtiness",0.20,pl)
				MoodModule.DecreaseMood("Fatigue",0.2,pl)
				print("Removing mood for jumping")
				task.wait(1)
			until
			jumping == false
		end
	end)
end)

Also, I am not sure why it stops taking away somebody’s mood when they stop running or jumping, but not walking.

I think it because that you repeat the loop until plhuman.WalkSpeed < 14 but it never happens so it won’t get out from the loop.

I mean, so this is your else-if entry

plhuman.WalkSpeed >= 16 and plhuman.WalkSpeed <= 18

but the exit condition is

plhuman.WalkSpeed < 14

What is the mechanic for getting slower walking speed?

WalkSpeed is not the same thing as player’s current speed, even the player stops moving, their WalkSpeed is the same. It is an indicator that used to indicate how fast players will walk when they do.

If you want to detect whether the player is moving, then use, MoveDirection

Move direction would be the direction in which a player is moving tho if I am correct.

Yes, but if the value inside MoveDirection is zero, it means that the player stops moving so you can exit your walking loop from there I suppose?

Why a repeat? Why not a while loop? Even better, runservice.

Do you have something changing WalkSpeed … that’s not going to change as you slow down or stop walking.

I have changed my code, deciding not to remove any moods from the player for walking and instead only for running and jumping, and instead of walking it will consistantly remove a player’s mood a bit.

(script works with the changes)

(also utilizing some of what @weakroblox35 had said)

pl.CharacterAdded:Connect(function(char)
	local plhuman = char:WaitForChild("Humanoid")
	print(char.Name)
	print(plhuman.Name)
	plhuman:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
		while plhuman.WalkSpeed >= 18 do
			MoodModule.DecreaseMood("Thirst",0.04,pl)
			MoodModule.DecreaseMood("Hunger",0.015,pl)
			MoodModule.DecreaseMood("Dirtiness",0.020,pl)
			MoodModule.DecreaseMood("Fatigue",0.04,pl)
			print("removing mood for running")
			task.wait(1)
		end
	end)
	plhuman:GetPropertyChangedSignal("Jump"):Connect(function()
		if plhuman.Jump == true then
			MoodModule.DecreaseMood("Thirst",0.3,pl)
			MoodModule.DecreaseMood("Hunger",0.015,pl)
			MoodModule.DecreaseMood("Dirtiness",0.20,pl)
			MoodModule.DecreaseMood("Fatigue",0.2,pl)
			print("removing mood for jumping")
			task.wait(1)
		end
	end)
	while true do
		MoodModule.DecreaseMood("Thirst",0.05,pl)
		MoodModule.DecreaseMood("Hunger",0.003,pl)
		MoodModule.DecreaseMood("Dirtiness",0.065,pl)
		MoodModule.DecreaseMood("Fatigue",0.02,pl)
		task.wait(3)
	end
end)

Small tip: You can have a real-time console window if you want. Just press that button in the view tab.

I know, but its a little easier for me to use the dev console sometimes to.

A more performance optimized version would go as follows

My Approach

local RunService = game:GetService("RunService")

pl.CharacterAdded:Connect(function(char)
    local plhuman = char:WaitForChild("Humanoid")
    local isMoving = false -- flag to track if the player is moving
    
    plhuman:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
        while plhuman.WalkSpeed >= 18 do
            MoodModule.DecreaseMood("Thirst",0.04,pl)
            MoodModule.DecreaseMood("Hunger",0.015,pl)
            MoodModule.DecreaseMood("Dirtiness",0.020,pl)
            MoodModule.DecreaseMood("Fatigue",0.04,pl)
            print("removing mood for running")
        end
    end)
    
    plhuman:GetPropertyChangedSignal("Jump"):Connect(function()
        if plhuman.Jump == true then
            MoodModule.DecreaseMood("Thirst",0.3,pl)
            MoodModule.DecreaseMood("Hunger",0.015,pl)
            MoodModule.DecreaseMood("Dirtiness",0.20,pl)
            MoodModule.DecreaseMood("Fatigue",0.2,pl)
            print("removing mood for jumping")
        end
    end)

-- create a function to handle mood decrease when player is moving
local function decreaseMoodWhileMoving()
    while isMoving do
        MoodModule.DecreaseMood("Thirst",0.05,pl)
        MoodModule.DecreaseMood("Hunger",0.003,pl)
        MoodModule.DecreaseMood("Dirtiness",0.065,pl)
        MoodModule.DecreaseMood("Fatigue",0.02,pl)
        task.wait(3)
    end
end

-- connect to the player's movement events
plhuman.MoveDirectionChanged:Connect(function(moveDirection)
    if moveDirection ~= Vector3.new() then
        isMoving = true
        decreaseMoodWhileMoving()
    else
        isMoving = false
    end
end)

plhuman.Jumping:Connect(function()
    isMoving = false
end)

plhuman.FallingDown:Connect(function()
    isMoving = false
end)

RunService.Stepped:Connect(function()
    if isMoving then
        decreaseMoodWhileMoving()
    end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.