Script teleports when it's not supposed to?

I have this script inside a portal door where you have to own the specific 5 badges mentioned in the script for the door to be open “door:Destroy()”, but for some reason it opens even though I don’t own the badges, how do I fix this issue?

Current script:

local Badges = {2147239897, 2147239900, 2147239904, 2147239910, 2147239915} -- Put your badgeID's here
local TeleportService = game:GetService("TeleportService")
local BadgeService = game:GetService("BadgeService")
local userHasBadge = BadgeService.UserHasBadgeAsync
local awardBadge = BadgeService.AwardBadge
local door = game.Workspace.AccessDoor
local icons = game.Workspace.BadgeIcons



script.Parent.Touched:Connect(function(obj)
	if obj.Parent:FindFirstChild("Humanoid") then
		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
					door:Destroy()
					icons:Destroy()
					--[[
					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
end)

Try this and tell me if it works


local Badges = {2147239897, 2147239900, 2147239904, 2147239910, 2147239915} -- Put your badgeID's here
local TeleportService = game:GetService("TeleportService")
local BadgeService = game:GetService("BadgeService")
local userHasBadge = BadgeService.UserHasBadgeAsync
local awardBadge = BadgeService.AwardBadge
local door = game.Workspace.AccessDoor
local icons = game.Workspace.BadgeIcons



script.Parent.Touched:Connect(function(obj)
	if obj.Parent:FindFirstChild("Humanoid") then
		for _,badge in pairs(Badges) do
			local success, result = pcall(userHasBadge, BadgeService, game.Players:GetPlayerFromCharacter(obj.Parent).UserId, badge)
			if success then

				if result then —- removing the not can maybe fix this
					door:Destroy()
					icons:Destroy()
					--[[
					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
end)

1 Like

The door doesn’t let me through anymore since I don’t own the badges so I assume it’s working.

Thank you so much!!

Based on what the OP requested, I believe the player needs to own all 5 of the badges in order for the code to proceed.

With the OP’s and your version of the code, the code proceeds further even if the player owns at least 1 of the badges in the Badges table instead of requiring all 5.

Hence, to simply add to that and assuming this is what @Ghostinee intended to achieve, the below code should yield the result you’re expecting, making sure the player owns all 5 badges and only then the door will be destroyed with an added table debounce to assure the Async call isn’t redundantly spammed until finished its iteration.

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

local door = workspace.AccessDoor
local icons = workspace.BadgeIcons

local Badges = {2147239897, 2147239900, 2147239904, 2147239910, 2147239915} -- Put your badgeID's here
local dbs = {}

local function hasBadges(plr)
	for _,badge in pairs(Badges) do
		local success, hasBadge = pcall(function()
			return BadgeService:UserHasBadgeAsync(plr.UserId, badge)
		end)
		
		if success and not hasBadge then
			return false
		end
	end
	
	return true
end

script.Parent.Touched:Connect(function(obj)
	local plr = Players:GetPlayerFromCharacter(obj.Parent)
	
	if plr then
		if not dbs[plr.Name] then
			dbs[plr.Name] = true
			
			if hasBadges(plr) then
				print(plr.Name.." has all the required badges.")
				door:Destroy()
				icons:Destroy()
			else
				print(plr.Name.." does NOT have all the required badges.")
			end
			
			dbs[plr.Name] = false
		end
	end
end)
2 Likes

Oh, I didn’t pay attention that well… :worried:
Thank you so much!!

1 Like

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