Trying to award a badge after touching a part

  1. Award a badge after touchinf a part
  2. UserId is not a valid member of Part "Workspace.Jai_2den.Right Leg
local part = game.Workspace.Lobby.Spleef.Badgegiver
local badgeid = 2149792693
local badge = game:GetService("BadgeService")
part.Touched:Connect(function(player)
print("Floor touched")
	if not badge:UserHasBadge(player.UserId, badgeid) then
		badge:AwardBadge(player.UserId, badgeid)
		print("Badge given")
	end
end)
1 Like

The issue you have is that you’re assuming every thing that touches the part is a player. For the most part, this wouldn’t be a problem as long as it’s hovering and there isn’t flying parts in your game, but if its a giant, invisible block, it may be touching some parts on the side.

The fix to this is to check if the part hit, which you’ve labelled “player” even though that isnt the case, is the player.

part.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild("Humanoid") then 
       local player = hit.Parent:FindFirstChild("Humanoid")
       print("Floor touched")
       if not badge:UserHasBadge(player.UserId, badgeid) then
	        badge:AwardBadge(player.UserId, badgeid)
	       	print("Badge given")
       end
	end
end)

So, I tried this and this popped up
image


that is what is surronding

use this instead!

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then 
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			print("Floor touched")
			if not badge:UserHasBadge(player.UserId, badgeid) then
				badge:AwardBadge(player.UserId, badgeid)
				print("Badge given")
			end
		end
	end
end)
2 Likes

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