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

Script works fine client side, but not server side?

Asked by 9 years ago

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

0
DataStores can only be accessed from server scripts. ImageLabel 1541 — 9y
0
Alright, where should I put this then? UnleashedGamers 257 — 9y
0
You would have to either use a server script or use Remote(Events, Functions) to communicate changes from clients to the server ImageLabel 1541 — 9y
0
A server script is a regular script correct? UnleashedGamers 257 — 9y
View all comments (6 more)
0
correct ImageLabel 1541 — 9y
0
Okay, what about the promptpurchase script should that be local or server? UnleashedGamers 257 — 9y
0
It doesn't matter whether it's client or server-side, IIRC ImageLabel 1541 — 9y
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" UnleashedGamers 257 — 9y
0
Check the answer I posted ImageLabel 1541 — 9y
0
Check my comment UnleashedGamers 257 — 9y

1 answer

Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

***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


0
Unkown global .."ID".. and .."PROD" UnleashedGamers 257 — 9y
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. ImageLabel 1541 — 9y
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. ImageLabel 1541 — 9y
0
Ok, I'll let you know if it works. UnleashedGamers 257 — 9y
View all comments (4 more)
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. UnleashedGamers 257 — 9y
0
I don't understand what you mean ImageLabel 1541 — 9y
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 UnleashedGamers 257 — 9y
0
It shouldn't be doubling the value... based on `CompletePurchase`, unless you have multiple different things going on at the same time ImageLabel 1541 — 9y
Ad

Answer this question