How do I detect whether the Humanoid belongs to a NPC or a Player

  1. So for my game, I want to make a brick that when a player touches it, it gives them a badge.

  2. The issue being, I also have NPCs around the map that can also touch the brick, which puts errors into the code whenever trying to find the UserId of the NPC (which isn’t a player)

  3. this is my current code

function onTouch(hit) 
	local character = hit.Parent
	if game.Players:WaitForChild(character.Name) ~= nil then
		local badgeservice = game:GetService("BadgeService")
		local id = ID
		if not badgeservice:UserHasBadgeAsync(game.Players[character.Name].UserId, id) then
			badgeservice:AwardBadge(game.Players[character.Name].UserId, id)
		end
	end
end

script.Parent.Touched:connect(onTouch)

so it gives out the badge but whenever an NPC touches it it keeps outputting Infinite Yield, is there a way to specifically check if the model is a player or NPC?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.