How can I make it so it only logs the datastore if the new value is less than the logged one?

I’m trying to make it so there’s a timer thing. If you beat your old record logged in the datastore, the old one gets deleted and the new one replaces it. Otherwise, nothing happens.

I’ve tried solutions for it, but they haven’t worked.

My current code:

ReplicatedStorage.SaveTimerEvent.OnServerEvent:Connect(function(player, labelText)
	local success, errorMessage = pcall(function()
		
		timerStore:SetAsync(player.UserId, labelText)
		
	end)

	if success then
		print("Successfully printed")
	else 
		if not success then
			warn("Error with printing. Error: ", errorMessage)
		end
	end
end)

Why don’t you try this:

  1. Find Old Value
  2. Compare Wanted Value with Old Value
  3. If Wanted Value is less than Old Value, Reset Old Value
2 Likes

Assuming you have some :GetAsync code to retrieve the data, you’d compare

if newValue > tonumber(labelText) then
    labelText = newValue
end

Then when on player leaves event:
timerStore:SetAsync(player.UserId, labelText)

Thank you very much. It works great now.

No problem, glad it helped! : )

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