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

Ontouch brick position with BadgeService?

Hello, I'm trying to do a script but it dont work, I want that when someone touches the brick check if you have the badge and run the movement. What is wrong with this script?

BadgeId = 484963036
 
script.Parent.Head.Touched:connect(function(hit)
    if game:GetService("BadgeService"):UserHasBadge(hit.userId, BadgeId) then
        script.Parent.Position = Vector3.new(20.063, 0.76, 24.606)
    else
        script.Parent.Position = Vector3.new(20.063, -10, 24.606)
    end
end)

1 answer

✓ Accepted answer

Since you made a duplicate question, I figured I would go ahead and answer the original.

The problem is that userId (and the preferred UserId) are members of Player objects.
In this code, hit is a Part object.

To fix this, use the GetPlayerFromCharacter function to find the player from the part, like this:

BadgeId = 484963036

script.Parent.Head.Touched:connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if Player then
        if game:GetService("BadgeService"):UserHasBadge(Player.UserId, BadgeId) then
            script.Parent.Position = Vector3.new(20.063, 0.76, 24.606)
        else
            script.Parent.Position = Vector3.new(20.063, -10, 24.606)
        end
    end
end)

1 comment

0

Thank you very much! it worked

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