I am making a new Roblox game and I want the player to touch a part and teleport to a different game but they need the specific badge to teleport.
Reason I ask is cause I am making a game where you have to go through levels and at the end of the level you get the badge.
and once they have the badge they go back to the main game and there is a teleporter that takes them to the next level but they need a badge to teleport.
This documentation (BadgeService) should be able to help you tell if a player has a certain badge or not. Let me know if you need extra help scripting this.
local Part = yourpart
local BadgeId = yourbadgeid
local PlaceId = yourplaceid
Part.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
if game.Players:FindFirstChild(Hit.Parent.Name) then
local plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
if game:GetService("BadgeService"):UserHasBadgeAsync(plr.UserId, BadgeId) then
game:GetService("TeleportService"):Teleport(PlaceId, plr)
end
end
end
end)
So, To get you started here, you would need to following Services:
BadgeService
This is so you can Access the Badges in your Game, Award them, and check if the Players have them
TeleportService
This is so you can Teleport to other games, If you are teleporting to a friends game, you will need to enable it by doing GameSettings > Security > Third Party Telepotation, I would recommend you have this off if you arent Teleporting to Third Party Games (aka Friends Games), This would only work if both games have this setting enabled. With this on, your game will be vulnerable to Attacks with Teleportation by Exploiters and Viruses.
To Begin, You will use BasePart.Touched, this will detect the Part that is touching it, but in this case, you will need to get the Player for the Teleport to work, This can be done on both Server and Client,
BasePart.Touched:Connect(function(part)
local Humanoid = part.Parent:FindFirstChild("Humanoid") -- Finds the First Humanoid
if Humanoid then -- if the Humanoid Exists
-- code will come
end
end)
for the Next Step, we will be using game.Players:GetPlayerFromCharacter(), what this will do is get the Player from the Character, If there is no Player under that character, it will return nil, if not, it will return a Player, but we will add an if statement to check if its actually a Player with part.Parent
if Humanoid then -- if the Humanoid Exists
local plr = game.Players:GetPlayerFromCharacter(part.Parent)
if plr then -- if there is actually a Player
-- code in a sec
end
end
Next thing we will do is Check if the Player Has a Badge by using BadgeService:UserHasBadgeAsync()
For the First Parameter, We will need to UserId, This will look through that Player to check if they have a Badge.
The Second Parameter, We need the BadgeId, This is so the Game knows what you are looking to check for, without it, it wouldn’t know what to check and will break the code.
BadgeService:UserHasBadgeAsync() returns as a boolean, so you will have to use an if statement:
if plr then
if BadgeService:UserHasBadgeAsync(plr.UserId, BadgeId) then
-- code in a sec
end
end
And Finally, we will use TeleportService:Teleport() or TeleportService:TeleportAsync(),
There isnt much of a Difference besides that TeleportAsync requires a List of Players.
This is how we will teleport our Player to the Game,
The First Parameter is the GameId or PlaceId
This is so the Game knows where to send the Player to
The Second Parameter is the Player
This is the Player that will be teleported
if BadgeService:UserHasBadgeAsync(plr.UserId, BadgeId) then
TeleportService:Teleport(GameId, Player)
end
Or:
if BadgeService:UserHasBadgeAsync(plr.UserId, BadgeId) then
TeleportService:TeleportAsync(GameId, {Player})
end
It Really depends on what you’re trying to do, It is likely that this code will fail to check correctly with the Player, Badge, and Teleportation, I would recommend looking into pcall() or protected calls
This is how your code should look: 999999 fills the void of PlaceId / GameId and BadgeId
local TeleportService = game:GetService("TeleportService")
local BadgeService = game:GetService("BadgeService")
BasePart.Touched:Connect(function(part)
local Humanoid = part.Parent:FindFirstChild("Humanoid") -- Finds the First Humanoid
if Humanoid then -- if the Humanoid Exists
local plr = game.Players:GetPlayerFromCharacter(part.Parent)
if plr then -- if there is actually a Player
if BadgeService:UserHasBadgeAsync(plr.UserId, 999999) then -- Checks badge
TeleportService:Teleport(999999, Player) -- Teleports Player
end
end
end
end)
If you want, you can use CollectionService to Handle Multiple Parts instead of having a Script for every single Part, CollectionService is better performance wise for this Case.
@Maytidis_good is also violating a rule by just giving out code, just so you know:
Please do not ask people to write entire scripts or design entire systems for you. If you can't answer the three questions above, you should probably pick a different category.
Just use an if statement with the PlayerHasBadgeAsync function, then you can use the TeleportService local Part = game.Workspace.Part
local BadgeId = 123 --The badge id
local PlaceId = 123 --The place id
Part.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild(“Humanoid”) then
if game.Players:FindFirstChild(Hit.Parent.Name) then
local plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
if game:GetService(“BadgeService”):UserHasBadgeAsync(plr.UserId, BadgeId) then
game:GetService(“TeleportService”):Teleport(PlaceId, plr)
end
end
end
end)