Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this part give you a badge on touch?

local badgeID =356818734  -->Put the badges ID there
local badgeService = game:GetService("BadgeService")
function onTouch(player)
	wait(1)
	badgeService:AwardBadge(player.userId, badgeID)
end
game.Workspace.Badge.Touched:connect(onTouch)

I was looking in the roblox wiki and I saw the scripts for how to make badges. I didn't see one for how to make a part give you a badge when you touch it, so I messed around with a badge script from the wiki. I tested it out and it says "userId is not a valid member of part". What does that mean and how do I fix it?

1 answer

✓ Accepted answer

The parameter of Touched is the part that touches the part with the touched event. You can use GetPlayerFromCharacter to get the player, which has the userID. Your script errors because "player" is actually a part, which does not have member called "userId", and not the player.

local badgeID =356818734  -->Put the badges ID there
local badgeService = game:GetService("BadgeService")
local debounce = false;
function onTouch(part)
	if part.Parent.Humanoid and not debounce then --if part is a member of a player's character
		debounce = true
    	local player = game.Players:GetPlayerFromCharacter(part.Parent)
    	badgeService:AwardBadge(player.userId, badgeID)
		wait(1)
		debounce = false
	end
end
game.Workspace.Badge.Touched:connect(onTouch)

The debounce is to prevent the event from rapidly activating.

This discussion is preserved as it was. New answers, comments, and votes are closed.