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

What is this confusing error and how to fix it? (Read all)

I have a game, and I have this code for purchasing a gamepass. But.... I got this confusing error when I try it. "11:59:59.999 - Players.Player1.PlayerGui.AdvertisementGUI.CoilGears1.LocalScript:6: attempt to index field 'Parent' (a nil value)". My code is:

if game.Players.LocalPlayer.userId > 1 then
	if game.MarketplaceService:PlayerOwnsAsset(game.Players.LocalPlayer, 578580632) then
		script.Parent:Destroy()
	end
end
script.Parent.MouseButton1Click:connect(function()
	game.MarketplaceService:PromptPurchase(game.Players.LocalPlayer, 578580632)
end)

Now, look at line 6.

script.Parent.MouseButton1Click --:connect(function() --I disconnected the code so the syntax isn't confused

Isn't "script" supposed to mean the script? Then why it shows that error? Also btw its a local script.

1 answer

✓ Accepted answer

You destroyed the script's parent in line 3, then tried to access it's parent (now nil) in line 6, which errors of course. I didn't know scripts could run with no parent though... well you probably want to delete the script as well with script:Destroy().

1 comment

0

Oh, thanks! I know how to fix my code now! (Basically by swaping the code order, and makes it so that the event disconnects before destroying it)

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