Badge-only hat giver not working

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.

Try moving the player setting variable into the hit function

local badgeId = 1749631489
debounce = true

function onTouched(hit)
	if (hit.Parent:FindFirstChild("Humanoid") ~= nil and debounce == true and game:GetService('BadgeService'):UserHasBadge(player.userId, badgeId)) then
        local player = game:GetService'Players':GetPlayerFromCharacter(hit.Parent)
		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)

I tried and got the error "Workspace.Badge.Script:5: attempt to index nil with ‘userId’ "

lemme try again:

local badgeId = 1749631489
debounce = true

function onTouched(hit)
    local player = game:GetService'Players':GetPlayerFromCharacter(hit.Parent)
	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)
1 Like

I don’t know what you did that time but it fixed it, thanks!

I didn’t realise you used the player variable in the if statement, so I moved it to the start of the function

1 Like