Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

DataStore can't be accessed from client?

Asked by 3 years ago

Hi There!

So i make a LocalScript to make a Developer Product

Local Script:

local productId = 1156283046
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
    game:GetService("MarketplaceService"):PromptProductPurchase(player, productId)
end)

local MarketplaceService = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
MarketplaceService.ProcessReceipt = function(receiptInfo)
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then
            if receiptInfo.ProductId == productId then
                player.leaderstats.Coin.Value = player.leaderstats.Coin.Value + 1000
            end
        end
    end
    local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.ProductId
    ds:IncrementAsync(playerProductKey, 1)  
    return Enum.ProductPurchaseDecision.PurchaseGranted

end

The ID is Correct

It wont work!

The output say:   20:26:17.366 DataStore can't be accessed from client - Client - LocalScript:9

Can Someone Help Me?

Thank's for Reading!

1 answer

Log in to vote
1
Answered by
danklua 72
3 years ago
Edited 3 years ago

You can use RemoteEvents https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events to tell the server when to save, so the main part of the script where it saves will be a normal Script not a localscript and on the script it would be:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)
    local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.ProductId
    ds:IncrementAsync(playerProductKey, 1)  
    return Enum.ProductPurchaseDecision.PurchaseGranted
end)

and make sure you prompt the purchase in the same script too so it doesnt say index with nil with reciptinfo. when you fire the remoteevent on the localscript you would do

script.Parent.MouseButton1Click:connect(function()
script.Parent.RemoteEvent:FireServer()
end)
0
Sorry i forgot to accept :P The_Saver31 260 — 3y
Ad

Answer this question