Calculating Consistent Score

I’m trying to add 100 score points per second to all players in game, The issue is that each round the total score points is different and not consistent.

This is an issue since I have multiplier system which increases players score points by a percentage and these inconsistencies can make huge differences after multipliers are applied.

Here is the code

	local roundStartTime = os.clock()
		local deltaTime = RunService.Heartbeat:Wait()
		for index, player in PlayersService:GetPlayers() do
			local scoreFolder = player:FindFirstChild("Score")
			if scoreFolder then
				local lastScore = scoreFolder:GetAttribute("Score")
				local newScore = lastScore + math.round(deltaTime * 100)
				scoreFolder:SetAttribute("Score", newScore) 
	until os.clock() - roundStartTime >= GameSettings.roundLength

I believe the issue is caused by the last line as the repeat loop doesn’t stop exactly when the condition is met but I’m not sure how to go about fixing it.

I’m not sure you need RunService.Heartbeat:Wait() since heartbeat runs every frame, but you’re adding a wait to it (approximately .03 seconds depending on lag).
Also, local newScore = lastScore + math.round(deltaTime * 100) is multiplying the change in time by 100 and adding it to the lastScore.
Multiplying by deltaTime would give the number of times your loop runs times the change in time every wait() since the loop started.
Shouldn’t it be
local newScore = lastScore + (100 * their percentage)

1 Like

The folder attribute name Score is used by client to update client UI, That’s why I’m updating it each frame, I’m not sure what you mean by local newScore = lastScore + (100 * their percentage ) could you provide extra details to how I should handle this?

You said you want to add 100 points * each player’s percentage every second.
Do you want this to give an update to their points while the round is running, or just at the end of the round?

I want to update the score value with percentage each frame during the round so it can update on the client since the client is using that value each frame not at the end of the round

I don’t think percentage matters now, I just need to get the base formula to be consistent each round and when I add percentage It would be consistent too since the base score is consistent.
I believe that the whole approach is not the one I should be implementing but I can’t figure out any better ones.

Let me simplify the problem.

I want to have a number that increases by 100 each second
and If I gave it a time of 10 seconds it will print exactly 1000 after 10 seconds have passed

But I need this number to update it’s value per frame.

If you need the value increased every one second why do you need to update every frame (about .03 of a second)?
Update it every 1 second of os.time instead of every wait(), similar to this post: Creating a timer that displays milliseconds.

1 Like

Update 3:

I just realized my approach was wrong from the very beginning, The issue is simply that the system doesn’t have any boundaries even though the final outcome can be calculated before it even happens.

In my case, instead of just adding to score value in a given time frame without giving the system any clues of what the end score after the timer runs out would be, I precalculated the end score based on an equation and made sure that the current results don’t exceed the end results.

Here is the equation:

local maxScore = (pointsPerSecond + (pointsPerSecond * multiplier )) * roundLength

With max value of what the score of the player should be at the end of the round,
I can easily just use math.clamp() to ensure that the newScore that is being calculated each frame doesn’t exceed that threshold.

Any etxtra multipliers that the player would collect throughout the gameplay can simply be added the equation of maxScore.

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