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
The first argument for :SetAsync()
is the unique key that is used to reference data. The second argument is that data.
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.