Unable to cast instance to int64 error

I have recently been trying to make a system where if the player in the game has a value greater than mine, they will be rewarded with a badge. However, when trying to compare my data from the datastore to the player’s current value, It comes up with an error, saying “Unable to cast instance to int64”

I have printed my data out in the output, and it reads as “104”, however, it still won’t compare the two numbers.

Here is the code:

local DataStoreService = game:GetService("DataStoreService")
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2397498599913242
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.Events.MoreUiEvents.MostTime

local CarlId = "Player_1822147878"
local Carl = PlayerData:GetAsync(CarlId)

if Carl then
	Event:FireAllClients(Carl[2])
else
	Event:FireAllClients(0)
end

Event.OnServerEvent:Connect(function(plr, Type, Time)
	if Type == "BoxWatch" then
		if Time >= Carl[2] then
			BadgeService:AwardBadge(plr, BadgeId)
		end
	end
end)

Any help is appreciated.

The error is in this line

BadgeService:AwardBadge(plr, BadgeId)

specifically plr

AwardBadge takes 2 parameters: userID (number) and badgeId (number). However, you are providing it with a user (Player / Instance) and the badgeId (number)

If you replace

BadgeService:AwardBadge(plr, BadgeId)

with

BadgeService:AwardBadge(plr.UserId, BadgeId)

it should resolve your issue

Just to let you know that you need to wrap these two lines in pcall()s
They are network requests so they can fail

For the first one:

local Carl
local success, err = pcall(function()
    Carl = PlayerData:GetAsync(CarlId)
end)
if success then
    --do stuff because it worked
elseif not success and err then
    --do stuff because it failed
end

and the second one:

local success, err = pcall(function()
    BadgeService:AwardBadge(plr, BadgeId)
end)
if success then
    --do stuff because it worked
elseif not success and err then
    --do stuff because it failed
end