Can anyone help me with this script!

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)
2 Likes

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.

3 Likes

hi I used your code and it didn’t work I looked at the developer logs and saw this

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.
Screenshot 2023-01-08 at 17.12.57

1 Like

I figured it out that I made a small mistake. Change TeleportService:Teleport(12011559418, Player.UserId) to TeleportService:Teleport(12011559418)

Edit: I fixed the code in my post above.

1 Like

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.

you can also put break after setting Pass to false to exit the for loop.

1 Like

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)
1 Like

Edited. Thanks. I was in a hurry when I typed the code so I didn’t have the time to check it carefully.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.