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

Attempt to index with nil UserId? Please help

So here is my code, I don't know what is wrong with it.

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

-- Directly after an in-game purchase with MarketplaceService:PromptGamePassPurchase(Player, GamepassId)

script.Parent.MouseButton1Click:Connect(function()
	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassId) and  Player.leaderstats.Points.Value > 499 then
		game.Workspace:WaitForChild(Player.Name).HumanoidRootPart.CFrame = workspace.TeleportTo1
		print("player owns gamepass")
	else
		game:GetService("MarketplaceService"):PromptGamePassPurchase(Player, GamepassId)
			print ("prompted purchase")
	end
end)

Getting an error about: " 19:01:42.467 - Players.WishXVI.PlayerGui.ScreenGui.Teleframe.TextButton.Script:8: attempt to index nil with 'UserId'""

2 comments

0

is this a local or server script?

0

ServerScript. In the error, you can see "TextButton.Script", which is the default name for a ServerScript. LocalPlayer can only be used in LocalScripts.

1 answer

I can assume you're using a ServerScript, as TextButton.Script is visible in the error.

LocalPlayer can't be used in ServerScripts. Instead, you'd have to use a PlayerAdded event to get the player.

game.Players.PlayerAdded:Connect(function(player)
	print(player.Name)
end)

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