I added a simple badge requirement clause to a commonly used hat giver script so that you are unable to get a certain hat unless you have said badge. I can confirm that the script worked perfectly without my edits. One error is appearing in output and I can’t figure out how to fix it. Any help would be greatly appreciated.
Error message: “Workspace.Badge.Script:2: attempt to index nil with ‘Parent’”
Initial script (working):
debounce = true
function onTouched(hit)
if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true) then
debounce = false
h = Instance.new("Hat")
p = Instance.new("Part")
h.Name = "domino hat"
p.Parent = h
p.Position = hit.Parent:findFirstChild("Head").Position
p.Name = "Handle"
p.formFactor = 0
p.Size = Vector3.new(2, 1, 1)
p.BottomSurface = 0
p.TopSurface = 0
p.Locked = true
script.Parent.Mesh:clone().Parent = p
h.Parent = hit.Parent
h.AttachmentPos = Vector3.new(0, -0.3, 0)
wait(5)
debounce = true
end
end
script.Parent.Touched:connect(onTouched)
My script (not working):
local badgeId = 1749631489
local player = game:GetService'Players':GetPlayerFromCharacter(hit.Parent)
debounce = true
function onTouched(hit)
if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true and game:GetService('BadgeService'):UserHasBadge(player.userId, badgeId)) then
debounce = false
h = Instance.new("Hat")
p = Instance.new("Part")
h.Name = "domino hat"
p.Parent = h
p.Position = hit.Parent:findFirstChild("Head").Position
p.Name = "Handle"
p.formFactor = 0
p.Size = Vector3.new(2, 1, 1)
p.BottomSurface = 0
p.TopSurface = 0
p.Locked = true
script.Parent.Mesh:clone().Parent = p
h.Parent = hit.Parent
h.AttachmentPos = Vector3.new(0, -0.3, 0)
wait(5)
debounce = true
end
end
script.Parent.Touched:connect(onTouched)
What line of code are you getting this error? The code doesn’t detect the thing you are trying to parent. Might not be loaded or you reffered to it incorrectly.