Need help with clickdetecter

function OnClick(part)
	if (part.Parent:FindFirstChild("Humanoid") ~= nil) then
		local p = game.Players:GetPlayerFromCharacter(part.Parent)
		if (p ~= nil) then
			print("Awarding BadgeID: " ..script.Parent.BadgeID.Value .. " to UserID: " .. p.userId)
			local b = game:GetService("BadgeService")
			b:AwardBadge(p.userId, script.Parent.BadgeID.Value)
		end
	end
end

script.Parent.ClickMe.Clicked:Connect(OnClick) -- script erored here
 

I dont know if this clicked is no longer a function or am i mispelling some words?
also how to make the brick instead of clicked also tappable with mobile?

2 Likes

Looks like you’re confusing clicked with touched a little bit here. If it were touched you could check for a humanoid in the part the touched the brick, but I believe in clicked your ‘part’ variable would actually return the player who clicked the object, not any part of their character, and thus there would be no point in checking for a humanoid.

1 Like

Nope, i dont understand what you mean

Start by changing clicked to MouseClick

1 Like

Thanks! now it works, also how to make it tappable while on mobile?

1 Like

I think it works for both, test using studio, in the upper right corner has a mobile icon, click on it and click in play

1 Like

I’m not too sure if it would work on mobile automatically or not, but you also need to change another part of your code. Right now you’re checking for a humanoid inside the player object and that is always going to not work. Your ‘part’ variable is literally the player object of the player who clicked the part. So I would rename it to Player or PlayerWhoClicked or something like that and then you can just drop the humanoid check and award the badge that way.

1 Like

ClickDetector is compatible with mobile.

1 Like

Here is how I would change your code.

local b = game:GetService("BadgeService")-- get the badge service first so it doesn't re-get it every time someone clicks (this may not really make a difference on performance)

function OnClick(Player)--I changed your 'part' variable to Player because on MouseClick it returns the player object so I also got rid of the check for humanoid
	if Player then --now just check that the player exists and grant them the badge
		print("Awarding BadgeID: " ..script.Parent.BadgeID.Value .. " to UserID: " .. Player.userId)
		b:AwardBadge(Player.userId, script.Parent.BadgeID.Value)
	end
end

script.Parent.ClickMe.MouseClick:Connect(OnClick)
1 Like