Help attempt to index nil with 'Visible'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make crate for hats andhat auto on player head
  2. What is the issue? Include screenshots / videos if possible!
    Workspace.Wood Crate.Script:47: attempt to index nil with ‘Visible’
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked over solutions on the developer hub, website and google but I could not find a way to fix my problem

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


local clickdetector = script.Parent.ClickDetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataModule = require(ReplicatedStorage:WaitForChild("DataModule"))
local hatModule = require(ReplicatedStorage:WaitForChild("HatModule"))
local hatname = ReplicatedStorage:WaitForChild("Hats")
local hatimgplace = game.StarterGui.CrateGui.HatsImage

local hatInventory = {}


local function ChosenHat(player)
	
	local playerGui = player:WaitForChild("PlayerGui")
	local CrateGui = playerGui.CrateGui
	local crateIcon = CrateGui.cratehatch
	
	local timer = tick()
	
	crateIcon.Visible = true
	
	while tick() - timer < DataModule.CrateOpeningLenght do
		wait(.1)
		crateIcon.Rotation = math.random(-9,9)
	end
	
	
	

	
	crateIcon.Visible = false
	local hatNumber = math.random(1,100)
	local hatfound = false
	local hat = nil
	while hatfound == false do
		for i,v in pairs(hatModule.rarities) do
			if math.random(1,v) == hatNumber then
				hatfound = true
				hat = i
			end
		end
	end
	
	
	crateIcon.Visible = false
	local hatImage = hatimgplace:FindFirstChild(hat)
	hatImage.Visible = true --error
	wait(2)
	hatImage.Visible = false -- also this I think
		
	
	return hat
	
end

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	
	if player.leaderstats.Robux.Value >= DataModule.CommonCrateCost  then
		player.leaderstats.Robux.Value = player.leaderstats.Robux.Value - DataModule.CommonCrateCost
		
		local hatOpened = ChosenHat(player)
		
		print(hatOpened)
		
		local hats = hatname:FindFirstChild(hatOpened):Clone()
		table.insert(hatInventory, hatOpened)
		print(hatInventory)
		hatOpened.Parent = player
	end
end)

did anybody know how to fix it ?

Thanks :slight_smile:

image
the i you put there is a number, and the v is the string name. My question is. do you really mean that the child name is a number?
image
If it is then maybe make it
hat = tostring(i)

i is a raritytable and v is a chance people gonna get. Here my module script

local Hat = ReplicatedStorage:FindFirstChild("Hats")

local hatModule = {}

hatModule.hats = { 
	
	["Legendary"] = {
		
		Hat["Dominus Astra"];
		Hat["Dominus Frigidus"];
		
	};
	
	["Rare"] = {
		
		Hat["Dominus Empyreus"];
		Hat["Dominus Infernus"];
		
	};
	["Uncommon"] = {
		
		Hat["Red Sparkle Time Fedora"];
		
	};
	["Common"] = {
		
		Hat["Domino Crown"]
		
	}
	
}

hatModule.rarities = { --I only add this
	
	["Legendary"] = 5,
	["Rare"] = 15,
	["Uncommon"] = 30,
	["Common"] = 50
	
	
}

hatModule.Sales = {
	
	["Dominus Astra"] = 4000;
	
	
}







return hatModule

can you check it if I forgot something or doing wrong at somewhere ?

dont mind the hatModule.Sales the script is not done yet

local hatImage = hatimgplace:FindFirstChild(hat)

hatImage returns nil thts why, perhaps hat; Legendary uncommon or anything doesnt exist in hagimgplace?

nope. It just a picture with some of limited item and the picture visible is false

image

Just like no other items if there item that I need to add please tell me

for i,v in pairs(hatModule.rarities) do
			if math.random(1,v) == hatNumber then
				hatfound = true
				hat = i
			end
		end

Tht i can correspond to any one of these only- Legendary, Common, Rare, UnCommon.

local hatImage = hatimgplace:FindFirstChild(hat)

An in this line u check if ur hatImg folder has a child named hat which- if u remember is i- and since i can only b Legendary, Common, Rare, UnCommon, it will return nil as ur hatImg folder has no child named the ones i mentioned above.

then how can I fix it ? sry Im a beginner I dont really know

Could u explain wt is this?

local hatname = ReplicatedStorage:WaitForChild("Hats")

its a file where i put all hat like domino crown, dominus or something

Replace while loop with this:

while hatfound == false do
		for i,v in pairs(hatModule.rarities) do
			if math.random(1,v) == hatNumber then
				hatfound = true
				hat = hatModule[i].Name
			end
		end
	end

If it doesn’t work cud u send me the folder containing ur hats; tht hatsFolder i enquired abt earlier.

it appear this error Workspace.Wood Crate.Script:60: Script timeout: exhausted allowed execution time

theres one more data that have open time lenght its 7 seconds

Run this in command bar:

settings().Studio.ScriptTimeoutLength = -1

still same result the error appeared again

Try this now:

while hatfound == false do
		for i,v in pairs(hatModule.rarities) do
			if math.random(1,v) == hatNumber then
				hatfound = true
				local str = hatModule[i].Name
				hat = str:gsub(" ", "")
			end
		end
	end

oof. when I try run this code settings().Studio.ScriptTimeoutLenght = -1 before i ran the game. The roblox studio is not responding