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

Script works fine client side, but not server side?

This script works fine in studio, but not at all in the real game?

This script is a localscript located in the button where you buy the credits

local player = game.Players.LocalPlayer
local player2 = game.Players.LocalPlayer.leaderstats


function CompletePurchase()
    wait(1)
   player.leaderstats.Cash.Value=player.leaderstats.Cash.Value+10

end

local MarketplaceService = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local productId = 23429504


MarketplaceService.ProcessReceipt = function(receiptInfo) 


    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then


            if receiptInfo.ProductId == productId then

                CompletePurchase()
            end
        end
    end 

    local playerProductKey = "plr_" .. receiptInfo.PlayerId .. "_pur_" .. receiptInfo.PurchaseId
    ds:IncrementAsync(playerProductKey, 1)  

    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

10 comments

0

DataStores can only be accessed from server scripts.

0

Alright, where should I put this then?

0

You would have to either use a server script or use Remote(Events, Functions) to communicate changes from clients to the server

0

A server script is a regular script correct?

0

correct

0

Okay, what about the promptpurchase script should that be local or server?

0

It doesn't matter whether it's client or server-side, IIRC

0

The script still isn't working in the server, I check my developer output and this is what I got : "attempt to index field 'local player' a nil value"

0

Check the answer I posted

0

Check my comment

1 answer

***I only edited the CompletePurchase function by adding a player argument ***

local DataStore = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local MarketService = game:GetService("MarketplaceService")
local productId = 23429504

function CompletePurchase(player)
	local stats = player:WaitForChild("leaderstats")
	local cash  = stats:WaitForChild("Cash")
	cash.Value  = cash.Value + 10
end

MarketService.ProcessReceipt = function(receiptInfo) 
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then
			local ID,PROD = receiptInfo.PlayerID, receiptInfo.PurchaseId 
			
            if receiptInfo.ProductId == productId then
                CompletePurchase(player)
            end
        end
    end 

    local playerProductKey = "plr_" .. ID .. "_pur_" .. PROD
    DataStore:IncrementAsync(playerProductKey, 1) 
 
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end


8 comments

0

Unkown global .."ID".. and .."PROD"

0

You can disregard that, it's only a little problem with ROBLOX Studio's static analyzer.. It will not affect the code at all, just run it.

0

You can also remove keyword 'local' located in front of "ID, PROD" .. but you'll get W003... so might as well just keep it the way it is.

0

Ok, I'll let you know if it works.

0

It kinda works, when I first buy the dev product it gives me +10 credits, after that it goes from +20 to +40 and so on.

0

I don't understand what you mean

0

If I buy the product it should give me +10 credits, if I buy it again it gives me +20 then if I buy it again it gives me like +80 it should only give me +10 every time I buy it

0

It shouldn't be doubling the value... based on CompletePurchase, unless you have multiple different things going on at the same time

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