Unable to cast Instance to int64

Hello, I’m having this error when I try to use this script:

local badges = {2124888216, 2124888217, 2124888218, 2124888219, 2124888220, 2124888221, 2124888222, 2124888223, 
	2124888225, 2124888226, 2124888227, 2124888228, 2124888229, 2124888231, 2125962438, 2128054043, 2128054054
}
local badgeservice = game:GetService("BadgeService")

local totalValue = 0

game.Players.PlayerAdded:Connect(function(player)
	for i, badge in ipairs(badges) do
		if badgeservice:UserHasBadgeAsync(player, badge) then
			totalValue += 1
		end
	end
	player.leaderstats:WaitForChild("Obsticales").Value = totalValue
end)

print(totalValue)

I think the script itself is pretty self-explanatory, you check if they have the badge, if they do, raise the number by 1 and then get the total number. However, when I try this I get the error “Unable to cast Instance to int64”.

Are you sure the error is coming from this script? Because it should work fine.

Also, I would suggest moving total value and the print line to inside the function.

game.Players.PlayerAdded:Connect(function(player)
	local totalValue = 0

	for i, badge in ipairs(badges) do
		if badgeservice:UserHasBadgeAsync(player, badge) then
			totalValue += 1
		end
	end

	player.leaderstats:WaitForChild("Obsticales").Value = totalValue

	print(totalValue)
end)

What is Obsticales ClassName? It should be an IntValue (or any other instance that stores a number in their value).

The first argument is the player’s UserId.

1 Like

:UserHasBadgeAsync() needs the userid not the player

I’m very confused about this, is there anything I’m doing wrong here?

Thanks,
-Hobbes

You are likely adding a bad/nil value to totalValue, and adding any value to nil will yield an error.
You can resolve this error by first checking if totalValue is nil, and if it is, setting it to 0.
if totalValue == nil then
totalValue = 0
end

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