Hello, I am very new to scripting and I wanted to make a script that if you click a GUI button it will check if you need to have 4 badges and if you do have them it will teleport you to a place but if you don’t have the badges it won’t teleport you to the place.
local Badges = {2124775029, 2124888096, 2124895416, 2124895419}
local TeleportService = game:GetService("TeleportService")
local BadgeService = game:GetService("BadgeService")
local userHasBadge = BadgeService.UserHasBadgeAsync
local awardBadge = BadgeService.AwardBadge
script.Parent.MouseButton1Click:connect(function(ButtonPressed)
for _,badge in pairs(Badges) do
local success, result = pcall(userHasBadge, BadgeService, game.Players:GetPlayerFromCharacter(obj.Parent).UserId, badge)
if success then
if not result then
TeleportService:Teleport(12011559418,game.Players:GetPlayerFromCharacter(obj.Parent))
local success2, result2 = pcall(awardBadge, BadgeService, game.Players:GetPlayerFromCharacter(obj.Parent).UserId, badge)
if success2 then
if result2 then
end
end
end
end
end
end)
You’re currently teleporting the player 4 times. Try this.
local Player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local Badges = {2124775029, 2124888096, 2124895416, 2124895419}
local TeleportService = game:GetService("TeleportService")
local BadgeService = game:GetService("BadgeService")
script.Parent.MouseButton1Click:connect(function()
local Pass = true
for _,badge in pairs(Badges) do
if not BadgeService:UserHasBadgeAsync(Player.UserId, badge) then
Pass = false
end
end
if Pass == true then
TeleportService:Teleport(12011559418)
end
end)
This will check for all the badges then if the player has all the badges, it will teleport the player. Make sure this is a localscript.
Here’s how it looks in Roblox studio just in case you wanted to know.
I even tested publishing the game and clicking the button but it just gave the same error.
Put local Pass = true inside the function. If you don’t then you would have to rejoin after using it each time.
Also you don’t need the player, just the place id.
Hello. It is because when you’re pcalling, you are using this incorrectly. See below on how to properly use it. You are forgetting to define a function.
local success,result = pcall(function()
game.Players:GetPlayerFromCharacter(obj.Parent).UserId, badge)
end)