Player Teleports if They Do Not Own Badge

I’m currently making a game, and I was wondering how to make it to where if a player does not own a certain badge, they teleport to a different place.

Does anyone know how I could achieve this?

4 Likes

Using BadgeService, That Should work.

local badgeService = game:GetService("BadgeService")
local player = game.Players.LocalPlayer
local badgeId = id

if badgeService:UserHasBadgeAsync(player.UserId, badgeId) then
    --what you want here
end
1 Like

Try this:

-- Constants
local BadgeId    = nil -- replace with your badge id
local PlaceId    = nil -- replace with your game id

-- Services
local BadgeService = game:GetService("BadgeService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")

-- Variables
local LocalPlayer = Players.LocalPlayer

-- Events
Players.PlayerAdded:Connect(function(Player)
    if BadgeService:UserHasBadgeAsync(Player.UserId, tonumber(BadgeId)) then
      return true
else
      TeleportService:Teleport(PlaceId, Player)
      return false
   end
end)
1 Like

What type of teleportation? Game to game or one game but from area to area?

Teleporting from game to game.

What is the point of putting this in a local script and using game.Players.PlayerAdded?

1 Like

Start by breaking down the problem into smaller steps:

  1. When a player joins the game,
  2. check if they own a certain badge.
  3. If they do not,
  4. teleport them to a different place.

STEP 1
To solve step 1, we need to get a player.
If you want to do something with a Player as soon as they join, it would make sense to use the PlayerAdded event of the Players service. Other contexts will mean different approaches need to be considered (a Touched event, perhaps a GUI button is clicked, etc.) I am going to assume in this reply that you want to do this check when the player joins your game. We can start by writing a quick function to keep this organized:

Script in ServerScriptService (not LocalScript)

local function onPlayerAdded(player) -- player: the player that joined
    print(player.Name .. " joined the game!")
end

-- Create the event listener for a player joining the game
-- Will automatically pass in the player argument
-- to our function defined above
game.Players.PlayerAdded:Connect(onPlayerAdded)

STEP 2
Now that we can access a joining player and their properties, we need to handle step 2 inside the scope of the function with the player.
We will be appropriately using the BadgeService’s UserHasBadgeAsync function. We need to get the BadgeService and call this function, passing in correct arguments. We will be storing the results (true/false) of the function call in a variable called playerHasBadge.
It’s important to note that UserHasBadgeAsync is an asynchronous function and may return errors. It is better to handle these errors with a pcall() rather than ignore them. (example at the bottom of this page)

local BadgeService = game:GetService("BadgeService")
local BADGE_ID = 123456789 -- This will be the ID of the badge

local function onPlayerAdded(player) -- player: the player that joined
	-- All Players have a UserId property, we are using the player who joined the game
	local playerHasBadge = BadgeService:UserHasBadgeAsync(player.UserId, BADGE_ID)
	
end

-- Create the event listener for a player joining the game
-- Will automatically pass in the player argument
-- to our function defined above
game.Players.PlayerAdded:Connect(onPlayerAdded)

STEP 3
Now that you have a yes/no result of whether or not the player who joined your game owns a particular badge, it’s time to do something about it if they don’t own it.
To solve step 3, it’s as simple as comparing the value of our playerHasBadge variable to false. A quick way to do this is just negate the variable (true -> false, false -> true). Remember that conditional blocks will only be entered if the condition is true (in this case, we are hoping for if not false:

...
local function onPlayerAdded(player) -- player: the player that joined
	-- All Players have a UserId property, we are using the player who joined the game
	local playerHasBadge = BadgeService:UserHasBadgeAsync(player.UserId, BADGE_ID)
	if not playerHasBadge then
		-- If the player does not own the badge, this code block will be entered
	end
	-- If the player does own the badge, the if block above will be skipped
end
...

STEP 4
For the 4th and final step, we need to teleport the player that does not own a badge. This is where TeleportService’s Teleport function will come into play:

local BadgeService = game:GetService("BadgeService")
local TeleportService = game:GetService("TeleportService")
local BADGE_ID = 123456789 -- This will be the ID of the badge
local PLACE_ID = 987654321 -- This is the place ID to teleport the player to

local function onPlayerAdded(player) -- player: the player that joined
	local playerHasBadge = BadgeService:UserHasBadgeAsync(player.UserId, BADGE_ID)
	if not playerHasBadge then
		print("Player does not own badge - Teleporting...")
		-- Teleport the player using the passed in player variable and defined PLACE_ID 
		TeleportService:Teleport(PLACE_ID, player)
	end
	-- If the player does own the badge, the if block above will be skipped
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

FINISH
That’s all there is to it! NOTE that this (full) example will only work for players joining your game. If you want this to work in some other context (when they click a button or step on a block, etc) you will have to extract the code from steps 2, 3, and 4 and repurpose your structure with different events or additional conditions.
If there any questions, please ask! I hope this is helpful, and I apologize it is so long, I like to be thorough and cover as many knowledge levels (to an extent) as I can.

8 Likes

Perfect, this is exactly what I was trying to accomplish, thank you!

1 Like

In case anyone is seeing this post in the future, here’s some useful information:

local Players = game:GetService('Players')


local function PlayerAdded(Player)
	print(('%s has joined the server.'):format(Player.Name))
end


-- Loops through existing players in case the player joins before PlayerAdded fires.
-- This can be caused by the usage of WaitForChild() above your event, or simply delays in general which happen from time to time.
local GetPlayers = Players:GetPlayers()
for i = 1, #GetPlayers do
	local Player = GetPlayers[i]
	coroutine.resume(coroutine.create(function()
		PlayerAdded(Player)
        -- Run this function for each player in parallel.
	end))
end
Players.PlayerAdded:Connect(PlayerAdded) -- Listen for new players.