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

"is of type nil" Error when testing a gamepass button on a brick?

Hello, I am a noob at LUA, I tried this in order to make a ScreenGui/TextButton offer a gamepass On-Click.

local Player = game:GetService("Players").LocalPlayer

local MarketplaceService = game:GetService("MarketplaceService") local Button = script.Parent

local function onClicked() MarketplaceService:PromptGamePassPurchase(Player,6255391) end

Button.MouseButton1Click:Connect(onClicked)

But I get this error message when I test it:

Error	MarketplaceService:PromptGamePassPurchase() player should be of type Player, but is of type nil

Does anyone know how to fix it?

2 answers

To fix this, don't use 'local player = game.Players.LocalPlayer' on Server Side Script but instead use the variable 'player' in functions to find the local player.

so the function should be:

local Button = part -- brick's identifier
local function onClicked(player)
MarketplaceService:PromptGamePassPurchase(player,6255391)
end
Button.ClickDetector.MouseClick:Connect(onClicked)

I am not the best at explaining but hopefully my context helps a lot!

What's wrong?

It's Lua, not LUA. It isn't an acronym. (lol) Anyways, your issue here is that you are trying to access game.Players.LocalPlayer on a server script. This is because there is no local player on the server. Which player are you referring to? There could be none, there could be 10 players.

Solution

It's quite simple to solve this issue. Use a local script instead of a server script. This shouldn't cause any issues, it seems like you're using a GUI button as opposed to a ClickDetector.

To create a local script, hover over the GUI in the Explorer tab, and click on the plus. Type "LocalScript" and it should appear. Click it and the local script will be created. All you have to do now is just copy the code from the previous script, paste it over to the local script, and then delete the previous script. Your issue should be fixed!

Remember to mark this as the answer if it answered your question!

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