Distance counter gets stuck after initial hit

Hello! For my game, I have a system where a score counter comes up based on how far the flung victim is from you.

The problem however is that after your first hit, the score counter stays the same as the previous hit. Video below: (You may want to skip around it takes me a little to demonstrate)


I have tried things such as resetting the point value, using a value that’s on the client but I can’t figure out how to reset it.

This involves a module script that then calls the client to begin the point scoring GUI on the client side and most of it is good to go! It’s just this one thing.

I will leave both the module script and the local script below for anyone who wants to help!
Module Script:

	local addingPoints = player:WaitForChild("AddingPoints")
	local distanceTraveled = Instance.new("IntValue")
	distanceTraveled.Name = "Distance"
	distanceTraveled.Parent = victimRootPart
	
	local function checkPoints()
		
		pointsEvent:FireClient(player, victimRootPart)
		
		addingPoints.Value = true
		
		if player then
			task.wait(0.2)
		end
		
		while addingPoints.Value == true do
			distanceTraveled.Value = (humanPlayer.Position - victimRootPart.Position).Magnitude
			
			if victimRootPart.Velocity.Magnitude < 0.1 or victimRootPart.Position.Y < -490 then
				addingPoints.Value = false
				print("Player has stopped moving")
				player.leaderstats.Score.Value += distanceTraveled.Value
				distanceTraveled:Destroy()
			end
			
			task.wait()
		end
	end
	checkPoints()
end

Local Script:

local addingPoints = player:WaitForChild("AddingPoints")

pointsEvent.OnClientEvent:Connect(function(victimRootPart)
	
	local distanceValue = victimRootPart:WaitForChild("Distance")
	local ticking = sfxFolder.Tick
	local tickingStop = sfxFolder.TickFinish
	task.spawn(function()
		if addingPoints.Value == true then
			pointTween:Play()
			repeat
				ticking:Play()
				task.wait(0.02)
			until addingPoints.Value == false
			
			local pointInfoFinish = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, true, 0)
			local pointPropertiesFinish = {
				["TextSize"] = 100,
				["Rotation"] = math.random(-10, 10),
				["TextColor3"] = Color3.fromRGB(math.random(0, 255),math.random(0, 255),math.random(0, 255)),
			}
			local pointTweenFinish = tweenService:Create(pointAmount, pointInfoFinish, pointPropertiesFinish)
			
			pointTweenFinish:Play()
			ticking:Stop()
			tickingStop:Play()
			task.wait(0.2)
			pointTweenOut:Play()
		end
	end)
	
	local function addPoints()
		pointAmount.Text = distanceValue.Value
	end
	
	if addingPoints.Value == true then
		runService.Heartbeat:Connect(addPoints)
	else
		pointAmount.Text = distanceValue.Value
		runService.Heartbeat:Disconnect(addPoints)
	end
end)

Why wouldn’t you calculate it entirely on a client and keeping server value for server independently?Also velocity is a outdated property and it must be avoided.
Also

local pointPropertiesFinish = {
				["TextSize"] = 100,
				["Rotation"] = math.random(-10, 10),
				["TextColor3"] = Color3.fromRGB(math.random(0, 255),math.random(0, 255),math.random(0, 255)),
			}

Better be rooted outside of function there so it will be created only once and will act as constant same with pointInfoFinish
Task.wait() also has a pretty huge overhead and may act bad for people with high FPS so use Runservice.Heartbeat:Wait() instead
This way thread will yield until current hearbeat resumption cycle
Same with function you are using in task spawn
Im pretty sure you can cache it and im confused as to why you are using it in first place if connections already have own thread? :thinking: