You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make crate for hats andhat auto on player head
What is the issue? Include screenshots / videos if possible!
Workspace.Wood Crate.Script:47: attempt to index nil with ‘Visible’
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)
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?
If it is then maybe make it hat = tostring(i)
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.
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.
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