So, I’m making a dev product where, when you buy it, it kicks the player. But I keep getting an error saying, ServerScriptService.Test:6: attempt to index nil with ‘Kick’
Here’s the code:
local MPS = game:GetService(“MarketplaceService”)
local player = game.Players.LocalPlayer
MPS.ProcessReceipt = function(receiptInfo)
if receiptInfo.ProductId == REDACTED then
player:Kick()
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
It says that the player doesn’t exist yet, or didn’t load. Meaning you either are using a server script ( Which would make the game.Players.Localplayer not work) or the player doesn’t have time to load.
You need to reference your player in a different way. Assuming you’re using a TextButton (or ImageButton) to purchase this DevProduct, you could fire a RemoteEvent from a LocalScript then pick it up on the server:
-- LocalScript inside of a TextButton or ImageButton
local RemoteEvent = -- path to your RemoteEvent
script.Parent.MouseButton1Click:Connect(function()
RemoteEvent:FireServer()
end)
-- ServerScript in ServerScriptService
local RemoteEvent = -- path to your RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(player) -- here is your player
-- rest of your code
end)
Try this code instead. Put this code on a server script by the way and prompt the dev product purchase on a local script. I think it should work.
MPS.ProcessReceipt = function(receiptInfo)
if receiptInfo.ProductId == REDACTED then
local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
plr:Kick()
end
return Enum.ProductPurchaseDecision.PurchaseGranted;
end