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

attempt to index nil with 'Parent'?

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)

2 comments

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

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

2 answers

✓ Accepted answer

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

1 comment

0

Yeah super weird it wasn't cloning it so I moved it out of ReplicatedStorage and into the Workspace and it started working

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

2 comments

0

It didn't work, I got the same error

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?

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