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

How can I make a teleporter work if you own a badge?

Asked by 2 years ago

So I have a part that once you touch you teleport to another world, I used this script for it:


local TeleportService = game:GetService("TeleportService")

local placeID = 8951064375

local function onPartTouch(otherPart) local player = game.Players:GetPlayerFromCharacter(otherPart.Parent) if player then TeleportService:Teleport(placeID,player) end end

script.Parent.Touched:Connect(onPartTouch)

So, I'm trying to make another teleporter where if you touch the part you also get teleported to another world, but first, you need to own some specific badges from the game to be able to teleport, how can I make it work?

Also, please explain thoroughly as I'm a very new and bad scripter lol.

1 answer

Log in to vote
0
Answered by
ShaShxa 105
2 years ago
Edited 2 years ago

For that you can use BadgeService:UserHasBadgeAsync()

First we need 2 parameters, the player user id and the badge id. We will be also using pcall()

If we have both of them we can do following:

local BadgeService = game:GetService("BadgeService") -- Get service
local TeleportService = game:GetService("TeleportService")

local function onPartTouch(otherPart) 
    local player = game.Players:GetPlayerFromCharacter(otherPart.Parent) 

    local BadgeID = 0000000 -- Change this to your badge ID
    local placeID = 8951064375

    if player then
         local success, hasBadge = pcall(function() -- Run a pcall() to check if the user has badge or not.
            return BadgeService:UserHasBadgeAsync(player.UserId, BadgeID) -- check if user has badge and return either true or false
        end)

        if not success then -- check if it was not successful
            warn("Operation was not successful!") -- print a warning that it was not successful
        end

        if hasBadge then -- If the user has the badge
            TeleportService:Teleport(placeID,player) -- Teleport
        end
    end
end

If you have any questions, feel free to ask me! Make sure to mark this as answer if this was helpful.

-- If you want to check for multiple badges, you can do following:

local success, hasBadge = pcall(function()
        return BadgeService:UserHasBadgeAsync(player.UserId, BadgeID) and BadgeService:UserHasBadgeAsync(player.UserId, BadgeID_2) and BadgeService:UserHasBadgeAsync(player.UserId, BadgeID_3) -- And so on...
    end)

0
Thank you so much! Just wondering, if I want it to be multiple badges instead of just one, what edits should I do to the code? Ghostinee 27 — 2y
0
For than you can simply add an "and" and use a new BadgeId. example: return BadgeService:UserHasBadgeAsync(player.UserId, BadgeID) and BadgeService:UserHasBadgeAsync(player.UserId, BadgeID_2) ShaShxa 105 — 2y
0
Sorry for the late reply, thank you so much!! Ghostinee 27 — 2y
Ad

Answer this question