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

attempt to index nil with 'UserId'?

Quick overview! (I'm a fairly new to scripting, please bare with me!)

I have a GUI and a game pass, the idea is to have the player click the txt button and they get the tool whether they have the game pass or not, if not. Then they get prompted the game pass. But I cant seem to get it to work, and I've tried everything I know. So I'm here now.

This is the Local script inside the txt button for the game pass tool

local toolid = 13191691
local RE = game.ReplicatedStorage.RemoteEvent
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

script.Parent.MouseButton1Click:Connect(function(plr)
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, toolid) then
		RE:FireServer("ToolOwned")
	else 
		local function romptPurchase()
			local player = Players.LocalPlayer
			MarketplaceService:PromptProductPurchase(player, toolid)
		end
	end
end)

The problem with that is I keep on getting attempt to index nil with 'UserId' on line 7

Server script:

local RE = game.ReplicatedStorage.RemoteEvent


RE.OnServerEvent:Connect(function(plr, state)
	if state == "ToolOwned" then
		local tool = game.ReplicatedStorage.Weapons.KITCHEN
		local Clonedtool = tool:Clone()
		Clonedtool.Parent = plr.Backpack
	end
end)

Constructive criticism greatly apricated! This is my first time asking a question here, so I'm sorry if I did something wrong.

1 comment

0

Your player variable doesn't exist. I'm assuming you're referring to the variable passed to the function on line 6: plr. Replace player with plr and your script should run how it's intended to.

1 answer

✓ Accepted answer

The problem is with your local script, on line 6, you have plr as the first argument but if i am sure, MouseButton1Click does not return anything so the plr is nil, on line 7 you are referencing player variable, which is not even plr so event if plr variable would not be nil, you would still get error, what you should do is put line 11 at the start of the script, that way you will always have variable to the LocalPlayer, so should look like this:

local toolid = 13191691
local RE = game.ReplicatedStorage.RemoteEvent
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local player = Players.LocalPlayeer

script.Parent.MouseButton1Click:Connect(function()
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, toolid) then
		RE:FireServer("ToolOwned")
	else 
		local function romptPurchase()
			MarketplaceService:PromptProductPurchase(player, toolid)
		end
	end
end)

I also think that romptPurchase function you meant to name it promptPurchase.

1 comment

1

Haha, thank you! I understand a bit more now thanks to you, I also gotta get on those typos! But, that's what makes us better. For example, you had a typo of line 6, we all do it! :)

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