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

attempt to index nil with 'Parent'?

Asked by 4 years ago
Edited 4 years ago

So I have this script that puts a Gui into a player when touching a part and when I went to go test it today I got "attempt to index nil with 'Parent'" I literally changed nothing in the script and it was just working :/

local market = game:GetService("MarketplaceService")
    local part = script.Parent
    local GUI = game.ReplicatedStorage.GunShop

    local function onTouched(hit)
        local newboii = GUI:Clone()
        local humanoid = hit.Parent:WaitForChild("Humanoid")     
      if humanoid then
            local char = hit.Parent
            local sPlayer = game.Players:GetPlayerFromCharacter(char)
            local putinto = sPlayer:WaitForChild("PlayerGui")
        if market:UserOwnsGamePassAsync(sPlayer.UserId,8810446) then
            newboii.Parent = putinto -- this is the line it errors at 
        else
        market:PromptGamePassPurchase(sPlayer,8810446)
    end
        end
    end

    part.Touched:Connect(onTouched)
0
the very first thing that comes to mind when i look at the code is that(at line 6) it creates a new gui clone without identifying whether it is touching a player or not, so that would create alot of unnecessary cloned gui's in your game jediplocoon 877 — 4y
0
oh yeah I did have if putinto:FindFirstChild("GunShop") then return end and I thought that was causing the issue but it wasn't I just forgot to add it back in dradon08777 17 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Hey! Maybe try printing the name of the GUI that you are cloning. It could be that the GUI isn't actually being cloned.

0
Yeah super weird it wasn't cloning it so I moved it out of ReplicatedStorage and into the Workspace and it started working dradon08777 17 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

This might work

local market = game:GetService("MarketplaceService")
    local part = script.Parent
    local GUI = game.ReplicatedStorage.GunShop

    local function onTouched(hit)
        local humanoid = hit.Parent:WaitForChild("Humanoid")     
      if humanoid then
            local char = hit.Parent
            local sPlayer = game.Players:GetPlayerFromCharacter(char)
            local putinto = sPlayer:WaitForChild("PlayerGui")
        if market:UserOwnsGamePassAsync(sPlayer.UserId,8810446) then
            GUI:Clone().Parent = putinto--put the clone gui here and made it clone into "putinto"
        else
        market:PromptGamePassPurchase(sPlayer,8810446)
    end
        end
    end

    part.Touched:Connect(onTouched)

If this doesn't work let me know and I can try and find another solution

0
It didn't work, I got the same error dradon08777 17 — 4y
0
I recreated the whole thing on my computer and it works just fine, are you sure it checks to see if the user owns the game pass correctly? jediplocoon 877 — 4y

Answer this question