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

[ANSWERED] attempt to index local "player" (a nil value)?

I am making a GUI button where if you click it, you will be prompted with a gamepass. If you own it or buy it, it will show a new gui where you can choose a level (my game has free levels and paid access levels).

However, when I create the function, I get:

attempt to index local 'player' (a nil value)

Here is the code:

local debounce = false
local Id = 7985691 --My gamepass id

function onButtonClicked(player)
	if debounce == false then
	debounce = true
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 7985691) then
script.Parent.Click:Play()
game.Workspace.Sound:Stop()
game.Workspace.Sound2:Play()
script.Parent.Parent.Parent.PremLevelsGUI.Visible = true
script.Parent.Parent.Parent.PremInfo.Visible = true
script.Parent.Visible = false
script.Parent.Parent.House.Visible = false
script.Parent.Parent.HTP.Visible = false
script.Parent.Parent.Parent.Back.Visible = true
debounce = false
	else
		game:GetService("MarketplaceService"):PromptGamePassPurchase(player, 7985691)
	debounce = false
	end
end
	end


script.Parent.MouseButton1Click:connect(onButtonClicked)

Any help is much appreciated,thanks!

4 comments

0

i would also like the main code

0

Okay, it's right here

0

MouseButton1Click doesn't pass the player as an argument to its callback.

0

What do i use instead then?

1 answer

✓ Accepted answer

MouseButton1Click doesn't pass the player, because it can only be 1 player (the local player). So take that out (function onButtonClicked()), and just define the player at the top (local player = game.Players.LocalPlayer).

6 comments

0

How do i connect it to the function afterwards? In other words, i got rid of mouse button 1 click but what do i replace it with?

0

I never said get rid of MouseButton1Click. You need that. Just take out the 'player' parameter from your onButtonClicked function, and define player at the top of your script.

0

Okay thx

0

I get "attempt to index upvalue 'player' (a nil value)"

0

You're using a LocalScript, right? If not, you need to use a LocalScript for this.

0

It worked! Thanks so much :)

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