StunTime returning to 1 for some reason

StunTime is basically a variable that sets the duration of the stun. When they’re blocking im setting it to 5 so i can have like a block break effect, but for some reason it returns to 1.
Here is my code. (Ill cut it so you can see more clearly whats happening.)

		Hitbox.OnHit:Connect(function(hit, humanoid)
			if humanoid.Parent.Name ~= script.Parent.Parent.Name then
				local vel = Instance.new("BodyVelocity")
				vel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
				vel.Velocity = Character.HumanoidRootPart.CFrame.LookVector * 30
				vel.Name = "vel"
				vel.Parent = hit
				local brektrack = humanoid:LoadAnimation(brek)
				print(humanoid.Parent:WaitForChild("StunTime").Value)
				if humanoid.Parent.IsBlocking.Value == true then
					print(humanoid.Parent:WaitForChild("StunTime").Value.." 1")
					humanoid.Parent:WaitForChild("StunTime").Value = 5
					print(humanoid.Parent:WaitForChild("StunTime").Value.." 2")
					humanoid.Parent.stun.Value = true
					humanoid.Parent.IsBlocking.Value = false
					script.Parent.blockb:Play()
					brektrack:Play()
					wait(0.5)
					vel:Destroy()
				else
					humanoid.Parent.stun.Value = true
				end
				print(humanoid.Parent:WaitForChild("StunTime").Value.." 3")
				--7919154517
				script.Parent.spp:Play()
				
				humanoid:TakeDamage(15)
				humanoid.Parent.HumanoidRootPart.Orientation = Character.HumanoidRootPart.Orientation + Vector3.new(0,180,0)
				wait(humanoid.Parent.StunTime.Value)
				vel:Destroy()
				humanoid.Parent.stun.Value = false
				humanoid.Parent:WaitForChild("StunTime").Value = 1
			end
		end)
		wait(0.3)
		body:Destroy()
		wait(0.2)
		Hitbox:HitStop() 
		wait(1)
		db = false
		if IsBlocking.Value == false then
			Character.Humanoid.WalkSpeed = 16 
		end
		wait(5)
		
		spdb = false
	end
end)
1 Like

When printing is it correctly displayed as being changed to 5 before going back to 1?

1 Like
local debounce = false

Hitbox.OnHit:Connect(function(hit, humanoid)
	if humanoid.Parent.Name ~= script.Parent.Parent.Name then
		local char = humanoid.Parent
		local animator = humanoid:WaitForChild("Animator")
		local stuntime = char:WaitForChild("StunTime")
		local stun = char:WaitForChild("stun")
		local blocking = char:WaitForChild("IsBlocking")
		
		local vel = Instance.new("BodyVelocity")
		vel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		vel.Velocity = Character:WaitForChild("HumanoidRootPart").CFrame.LookVector * 30
		vel.Name = "vel"
		vel.Parent = hit
		
		local brektrack = animator:LoadAnimation(brek)
		print(stuntime.Value)
		
		if debounce then
			return
		end
		debounce = true
		if humanoid.Parent.IsBlocking.Value then
			print(stuntime.Value.." 1")
			stuntime.Value = 5
			print(stuntime.Value.." 2")
			stun.Value = true
			blocking.Value = false
			script.Parent:WaitForChild("blockb"):Play()
			brektrack:Play()
			task.wait(0.5)
			vel:Destroy()
		else
			stun.Value = true
		end
		print(stuntime.Value.." 3")
		--7919154517
		script.Parent:WaitForChild("spp"):Play()
		humanoid:TakeDamage(15)
		humanoid.Parent:WaitForChild("HumanoidRootPart").Orientation = Character:WaitForChild("HumanoidRootPart").Orientation + Vector3.new(0,180,0)
		task.wait(stuntime.Value)
		vel:Destroy()
		stun.Value = false
		stuntime.Value = 1
		debounce = false
	end
end)

I’ve added a debounce & fixed up some things. I think before the script was being executed too frequently, the debounce should prevent that.

2 Likes