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?

For some reason i get getting a Error: " Workspace.Shops.Shop1.Part.Script:5: attempt to index nil with 'Level' ", anyone know how to fix this? Here is the script

local ShopLevel = 1
local function onTouch(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr.Level.Value == ShopLevel or plr.Level.Value > ShopLevel then
	if plr.PlayerGui:FindFirstChild("ShopGui") then return end
	local gui = game.Lighting.GUIs:FindFirstChild("ShopGui")
	gui:Clone().Parent = plr.PlayerGui
	end
end

script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

Try this, and the hit may be nil! Please fix your code blocks!

local ShopLevel = 1
local function onTouch(hit)
     local model = hit:FindFirstAncestorOfClass("Model")
     if model then -- check if the model is there, the parent of most character parts.
          local plr = game.Players:GetPlayerFromCharacter(model)
          if plr then -- check if the model is really a player
               if plr:WaitForChild("Level").Value >= ShopLevel then -- easier way for the if statement
                    if plr:FindFirstChild("ShopGui") then return end
                    local gui = game.Lighting:FindFirstChild("ShopGui")
                    if gui then -- check if the gui is a real thing
                         local clonedGui = gui:Clone() -- clone the ui
                         clonedGui.Parent = plr.PlayerGui
                    end
               end
          end
     end
end
script.Parent.Touched:Connect(onTouch)

If this helps, please mark this as a solution! Metatable out!

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