Why does this happen?

Hello scripters! :smile:

This isn’t an issue but more of a curiosity to know something…

So I have made a text button with a local script that would prompt the player with a developer product purchase that would give them 500 Crystals with the help of a server-sided script of course so my question is in the server-sided script which is this code:

local MarketplaceService = game:GetService("MarketplaceService")

local function processReceipt(receiptInfo)
	local players = game:GetService("Players")
	
	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	player.leaderstats.Crystals.Value = player.leaderstats.Crystals.Value + 500
	
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = processReceipt

It works fine with absolutely no problems at all BUT when I add an () to the processReceipt which is at the very end like this:

MarketplaceService.ProcessReceipt = processReceipt()

Basically for some reason the script would break and it would also give me this error shown in the image:

errorwe

So my question is why does that happen when I do that? but it works fine when I remove the ()? what does the () do?

1 Like

Functions are first-class values.

What you’re doing is nothing out of the ordinary. Functions are datatypes like strings, booleans, etc. When you do x = f() you are assigning x to the result of f. ProcessReceipt should get a function, but your code never even gets to giving it the result of processReceipt since the exception was thrown. It is basically saying no argument was given.

1 Like

So your basically saying that because I didn’t put anything inside of the ()

The error is telling me that () doesn’t contain anything? can’t really understand it that well but if that’s the case why did it also mention ‘PlayerId’?

In this case yes

Because you attempted to index a nil value with…PlayerId.

receiptInfo.PlayerId

But since no argument was given it was more similar to (nil).PlayerId which you cannot do

1 Like

Okay thanks, you explained it well now I understand!