Help with stun module script

Recently I’ve been working on a battlegrounds game and I’ve had an annoying problem.

Well basically I have a module of functions involving the character and among the functions there is one to stun it.

The problem I’m getting is that sometimes even when calling the function passing the bool value of StunWalkSpeed ​​to true, the walk speed is not changed, just the Auto Rotate

function module.addStun(Character, StunTime, StunWalkSpeed, IFrame)
	StunTimes[Character.Name] = {
		StunOs = os.clock();
		StunWalkSpeed = StunWalkSpeed
	}

	Character:SetAttribute("IFrame", IFrame)
	Character:SetAttribute("Stunned", true)

	if StunWalkSpeed == true then
		Character.Humanoid.WalkSpeed = 0
		Character.Humanoid.JumpPower = 0

		Character.Humanoid.AutoRotate = false
	end

	task.delay(StunTime, function()
		if os.clock() - StunTimes[Character.Name].StunOs >= StunTime then
			Character:SetAttribute("Stunned", false)
			Character:SetAttribute("IFrame", false)

			if StunWalkSpeed then
				Character.Humanoid.WalkSpeed = 16
				Character.Humanoid.JumpPower = 50

				Character.Humanoid.AutoRotate = true
			end
		elseif not StunTimes[Character.Name].StunWalkSpeed and StunWalkSpeed == true then
			Character.Humanoid.WalkSpeed = 16
			Character.Humanoid.JumpPower = 50

			Character.Humanoid.AutoRotate = true
		end
	end)
end

Are you setting the .WalkSpeed anywhere else?

Yes, but I do a check to ensure that it will not restore if it is stunned

local function Run(_, InputState)
	if InputState == Enum.UserInputState.Begin and os.clock() - RunDelta > .25 and Running == false and not Character:GetAttribute("CombatStunned") and not Character:GetAttribute("Stunned") and not Character:GetAttribute("DashStunned") and Humanoid.MoveDirection ~= Vector3.zero then
		Running = true
		
		Humanoid.WalkSpeed = 22
		
		Animate.walk.WalkAnim.AnimationId = RunId

		local MoveDirectionChanged = Humanoid:GetPropertyChangedSignal("MoveDirection")

		repeat
			task.wait()
		until Humanoid.MoveDirection == Vector3.zero or Character:GetAttribute("Stunned") or Character:GetAttribute("DashStunned") or Character:GetAttribute("CombatStunned")
		
		if not Character:GetAttribute("Stunned") then
			Humanoid.WalkSpeed = 16
		end
		
		RunDelta = os.clock()
		Running = false

		local InCameraTween = TweenService:Create(Camera, TweenInfo.new(.1, Enum.EasingStyle.Linear), {FieldOfView = 70})
		InCameraTween:Play()

		Animate.walk.WalkAnim.AnimationId = WalkId
	end
end

Could you try to print the .WalkSpeed after you set it in your stun function?

do you talk about this part of the code?

if StunWalkSpeed == true then
		Character.Humanoid.WalkSpeed = 0
		Character.Humanoid.JumpPower = 0

		Character.Humanoid.AutoRotate = false
	end

Yes.

Well, I can insert this print, but the problem is something that happens considerably rarely, so I don’t know if I’ll be able to check what it prints.