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

DataStore can't be accessed from client?

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

✓ Accepted answer

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)

1 comment

0

Sorry i forgot to accept :P

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