Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED] Unable to cast Instance to int64?

Explanation: So I was trying to make a top donator leaderboard. However, I ended up encountering an error while trying to do so. I was trying to save the info of the donation that a player made into a DataStore so that the leaderboard updates. But when I put the second argument which was supposed to be the value of the DataStore in line 43, it gives me an error saying "Unable to cast Instance to int64". This may be because I got no clue how to write the second argument and so I tried to get the PriceInRobux and make it as a value for the argument by doing:

game:GetService("MarketplaceService"):GetProductInfo(--[[Some Arguments here]]).PriceInRobux

I know I was not supposed to do that. Does anyone have any idea how to write the second argument?

Here's the ServerScript named "DeveloperProductServer" located in ServerScriptService:

local MarketPlaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local RobuxDataStore = DataStoreService:GetOrderedDataStore("RobuxDateStore")
local RobuxDonateEvent = game:GetService("ReplicatedStorage"):WaitForChild("EventsFolder"):WaitForChild("RobuxDonateEvent")

local function UpdateLeaderBoard()
	local Success, ErrorMessage = pcall(function()
		local Data = RobuxDataStore:GetSortedAsync(false, 15)
		local RobuxDSPage = Data:GetCurrentPage()
		for RobuxDonated, data in ipairs(RobuxDSPage) do
			local Username = game:GetService("Players"):GetNameFromUserIdAsync(tonumber(data.key))
			local RobuxAmount = data.value
			local IsOnLeaderBoard = false
			for i, v in pairs(workspace:WaitForChild("GlobalLeaderboard"):WaitForChild("lbGUI"):WaitForChild("Holder"):GetChildren()) do
				if v.Player.Text == Username then
					IsOnLeaderBoard = true
					break	
				end
			end
			if RobuxAmount and IsOnLeaderBoard == false then
				local ClonelbFrame = game:GetService("ServerStorage"):WaitForChild("lbFrame"):Clone()
				ClonelbFrame.Player.Text = Username
				ClonelbFrame.Amount.Text = RobuxAmount
				ClonelbFrame.Position = UDim2.new(0, 0, ClonelbFrame.Position.Y.Scale + (0.08 * #workspace:WaitForChild("GlobalLeaderboard"):WaitForChild("lbGUI"):WaitForChild("Holder"):GetChildren()), 0)
				ClonelbFrame.Parent = workspace:WaitForChild("GlobalLeaderboard"):WaitForChild("lbGUI"):WaitForChild("Holder")
			end
		end
	end)
	if not Success then
		error(ErrorMessage)
	end
end
 
local function processReceipt(ReceiptInfo)
	local PlayerUserId = game:GetService("Players"):GetPlayerByUserId(ReceiptInfo.PlayerId)
	if not PlayerUserId then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	print("Player Donated Robux!")
	RobuxDonateEvent:FireClient(PlayerUserId)
	while true do
		for _, Player in pairs(game:GetService("Players"):GetPlayers()) do
			RobuxDataStore:SetAsync(game:GetService("Players"):GetNameFromUserIdAsync(PlayerUserId)) -- What Do I Write For The Second Argument??
		end
		for _, Frame in pairs(workspace:WaitForChild("GlobalLeaderboard"):WaitForChild("lbGUI"):WaitForChild("Holder"):GetChildren()) do
			Frame:Destroy()
		end
		UpdateLeaderBoard()
		wait(10)
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketPlaceService.ProcessReceipt = processReceipt

1 answer

✓ Accepted answer

The first argument for :SetAsync() is the unique key that is used to reference data. The second argument is that data.

Note:

You’re actually supposed to use the UserId Instead of the Name, as that is not unique and most definitely not static. If there comes the case where then Player changes their name, they will no-longer be able to reference their data since their Name doesn’t match the previous entry; another storage is opened up too to store the new data at their new name.


In your case, as discussed in the comments below, you shouldn’t be using :SetAsync() in this scenario. Since you want to continuously add to the RobuxAmount donated, we should use the :IncrementAsync() method instead.

To add on the Robux used in said donation, we can use ProcessReceipt.CurrencySpent to add onto their total donation number.

local UserId = ProcessRecipt.PlayerId
DataStore:IncrementAsync(UserId, ProcessReceipt.CurrencySpent)

You also should iterate through every Player in the game and run this line, as it will actually update their Data to the same information, simultaneously making it seem as if everyone suddenly donated the same amount.

19 comments

0

Oo understood, since im trying to make a top donator leaderboard here, what data can i use?

0

What data were you planning on storing if you used a DataStore in the first place?

0

the info of the donation player made

0

Then store that

0

how do i write the code?

0

Can you show me where the donation info is? Your script is unnecessarily complex and it’s a little late so I’m particularly tired, I apologize if I seem a tad bit rude but I’m still here to help with as much as I can

0

So basically, im trying to make it so that when a player pressed a donate button, it will prompt the purchase and if success, then the leaderboard will get updated if they donated enough robux

0

Here's how my explorer looks like: https://ibb.co/b5VczvN & https://ibb.co/gTRLMxS

0

Here's a picture of the donation Frame: https://ibb.co/KxXZtvG

0

After a load of research, and analysis of your program—of which I take back my previous claim—I finally have what you need! You’ll want to use :IncrementAsync() to handle this operation, as you’re adding to the data. After reading up on ProcessRecipts, I noticed a piece of information that is fundamental to your attempt.

0

There is a key within the Receipt Table titled CurrencySpent. Since you seem to be loading information into a variable named RobuxAmount, I assume this will protrude  to total amount donated, so I thought that using CurrencySpent is what we need.

0

it works but prints 0 when i try to print the CurrencySpent. nani? I literally "donated" 25 robux (it's a test purchase).

0

Sorry for the late reply because I didn't receive any notifications and I just saw your reply when I check just now

0

I believe that’s because no actual currency was spent, or it’s a purchase within Studio.

0

oo then may I also know why the "UpdateLeaderBoard()" function in line 48 is not working? I tried to print("Updating Leaderboard..") when the function received the signal but it's not printing.

0

@Feahren hey man you there?

0

I’m not sure:/

0

Alright, thanks for your help anyway :)

0

You are gonna wanna use this for ze leaderboard https://developer.roblox.com/en-us/api-reference/class/OrderedDataStore

This discussion is preserved as it was. New answers, comments, and votes are closed.