Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
1
Answered by
cabbler 1942 Moderation Voter
7 years ago

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().

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) MinePlayersPE1 33 — 7y
Ad

Answer this question