How would I compare datastore data?

Hello!
I have been trying to compare DataStore data and I could not figure out how. Please help!

script

for i, v in pairs(game.Players:GetPlayers()) do
	local Data = game:GetService("DataStoreService"):GetDataStore("MonthlyReignLeaderboard V:"..script.Parent.Version.Value)
	local Stats = game.ServerStorage.PlayerData:WaitForChild(v.Name).CurrentReign.Value
		if Stats then
			pcall(function()
				if Data:GetAsync(v.UserId) < Stats then
					DataStore:UpdateAsync(v.UserId, function(Value)
					return tonumber(Stats)
					end)
				end
			end)
		end
	end	
1 Like

Are you getting any errors ?
Is the Data in the Data Store a table ?
Is the “Stats” Variable the same type of data as the data store

also i would recommend using a variable for the data such as:

RetrievedData = Data:GetAsync(v.UserId)

no it doesn’t say im getting any errors.

Could you try using “print()” statements and screenshot your output ?

btw add a “print()” statements after these:
the “for” loop
the “if Stats then”
the “pcall(function()”
the “if Data:GetAsync(v.UserId) < Stats then”

k I will try to see if that works and what doesn’t get printed.

All of the prints work except for the last if statement.

May i see your output ?
Also could you change the “print()” statement before the last “if” statement to

print(Data:GetAsync(v.UserId))

the output is blank when I print

EDIT: it is actually nil

Oh you need to define something to print

print("Stage1")
print("Stage2")

etc
try that and see what happens
edit: disregard this

Have you saved any data to the data store ?

I only have some of my data stored on this leaderboard.

Is it a table ?
Are you sure its saved ?
if the “print(v.UserId)” is returning nil then you have no saved data

It prints my UserId when I print

Oh sorry i meant

print(Data:GetAsync(v.UserId))

I printed that alr and It got nil

You should check if you have any saved data, or assign a default value to it, otherwise it will obviously be nil as there isn’t any data stored in the datastore key.

other data r saved because it appears on the leaderboard.

It seems like you are calling the tonumber function on Stats. Is it possible that when you are doing Data:GetAsync(v.UserId) < Stats, you are comparing two different data types?

If so that would cause an error. To check for errors in a pcall, you have to get the return value.
So you should do local success, message = pcall(function()...and print the success and message.

ok I will try that.

btw the to number is to check if there is any decimals

when I did this it didn’t print our an error message so I dont think there is a problem with the to number.