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

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

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

✓ Accepted answer

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)

3 comments

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?

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)

0

Sorry for the late reply, thank you so much!!

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