Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to verify UserHasBadge before TeleportService?

Asked by
TofuBytes 500 Moderation Voter
10 years ago

If the user has the badge, they'll be teleported to the next place. If they don't, they will be denied access. I'm lost on what I forgot to add into the script. Any help on this?

BadgeId = 106566897 --Testing Badge


function onTouched(hit)
    character = hit.Parent
    if character then
        if game:GetService("BadgeService"):UserHasBadge(p.userId, BadgeId) then
        print("The user has this badge")
        print("Teleporting user to next level")
        game:GetService("TeleportService"):Teleport(158451458, player)
    else
        print("The user does not have this badge")
        print("Teleport disabled")
        end
    end
end

2 answers

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 years ago

Well, you want to define the player, so what I do is find the player's name in PlayerService. But you also don't want a random object hitting the brick messing your script up.

BadgeId = 106566897 --Testing Badge


function onTouched(hit)
    character = hit.Parent
    if character then
if game.Players:FindFirstChild(character.Name) then --Need to see if it is a player or random object.
local player=game.Players:FindFirstChild(character.Name)
        if game:GetService("BadgeService"):UserHasBadge(player.userId, BadgeId) then
        print("The user has this badge")
        print("Teleporting user to next level")
        game:GetService("TeleportService"):Teleport(158451458, player)
    else
        print("The user does not have this badge")
        print("Teleport disabled")
        end
    end
end
end
Ad
Log in to vote
1
Answered by
Mowblow 117
10 years ago

The reason your's is not working is because it is not referencing a character in the script. Hit.Parent may be a model or a gun. To do this you must reference a character. As so:

local char = Hit.Parent:FindFirstChild("Humanoid")
local plyr = game.Players:GetPlayerFromCharacter(Hit.Parent)
if char ~= nil and plyr ~= nil then -- ~= nil means not equal to nil.
--Rest of script
end

Answer this question