Remote Script wont detect if player has badge or gamepass

I am making a remote script where the player joins it will detect if the player has a badge or a gamepass. Everything else in the remote script works fine its just I need it to disable a script if the player does NOT have the badge or gamepass. I tried looking for hours but nobody responded. Here is the script:

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local badgeid = script.Parent.Parent:WaitForChild("BadgeID")
local gamepassid = script.Parent.Parent:WaitForChild("GamepassID")
local MarketplaceService = game:GetService("MarketplaceService")

local function playerGates(player)
	local Character = player.Character

	local HasBadge, Result = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeid)
	end)
	local HasGamepass, Result2 = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassid)
	end)

	if HasBadge == false then
		game.Workspace.Gate2.TouchGateScript.Enabled = false
	end
end


local event = game.ReplicatedStorage.JoinEvent

event.OnServerEvent:Connect(function(player)
	playerGates(player)
end)
3 Likes

Well, I think this should work.

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

local badgeid = script.Parent.Parent:WaitForChild("BadgeID").Value
local gamepassid = script.Parent.Parent:WaitForChild("GamepassID").Value

local function playerGates(player)
	local Character = player.Character

	local HasBadge, Result = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeid)
	end)

	local HasGamepass, Result2 = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassid)
	end)

	if not HasBadge or not HasGamepass then
		local gateScript = game.Workspace:FindFirstChild("Gate2") and game.Workspace.Gate2:FindFirstChild("TouchGateScript")
		if gateScript then
			gateScript.Enabled = false
		end
	end
end

local event = game.ReplicatedStorage.JoinEvent

event.OnServerEvent:Connect(function(player)
	playerGates(player)
end)
2 Likes

It almost worked but I set the gamepass and badgeid to random and the script is still active when I test it

Huh if this doesnt work then to be honest I have no idea what to do.

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

local badgeid = script.Parent.Parent:WaitForChild("BadgeID").Value
local gamepassid = script.Parent.Parent:WaitForChild("GamepassID").Value

local function playerGates(player)
    local Character = player.Character

    local HasBadge, BadgeResult = pcall(function()
        return BadgeService:UserHasBadgeAsync(player.UserId, badgeid)
    end)

    local HasGamepass, GamepassResult = pcall(function()
        return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassid)
    end)

    if not HasBadge or not HasGamepass then
        local gateScript = game.Workspace:FindFirstChild("Gate2") and game.Workspace.Gate2:FindFirstChild("TouchGateScript")
        if gateScript then
            gateScript.Enabled = false
        end
    end
end

local event = game.ReplicatedStorage.JoinEvent

event.OnServerEvent:Connect(function(player)
    playerGates(player)
end)

There might be an issue with how you handle the result from pcall. Other then that I have no clue.

I tested this in game and it still does not work. Do you think it is the id?

It could, but all I can think of is ensuring that the badge and the game pass ID are the same as the IDs you are using in-game.

You were checking if the pcalls succeeded, not the result of the pcalls.

Here is the script so far, I changed the gamepassid and badgeid so you can see it. But still does not work.

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

local badgeid = 0
local gamepassid = 0

local function playerGates(player)
	local Character = player.Character

	local HasBadge, BadgeResult = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeid)
	end)

	local HasGamepass, GamepassResult = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassid)
	end)

	if not HasBadge or not HasGamepass then
		local gateScript = game.Workspace:FindFirstChild("Gate2") and game.Workspace.Gate2:FindFirstChild("TouchGateScript")
		if gateScript then
			gateScript.Enabled = false
		end
	end
end

local event = game.ReplicatedStorage.JoinEvent

event.OnServerEvent:Connect(function(player)
	playerGates(player)
end)

Ohh, ensure that the badge and the game pass ID are the same as the IDs you are using in-game. I that’s your problem.

So I will just use an alt account to test it?

You’re checking if the pcalls succeeded, try switching it around to the result of the pcalls instead.

Yeah it still does not work. I used 2 accounts at the same time, 1 that does not have gamepass and badge and 1 that has both of it. They both have enabled scripts just like before when I switched the gamepassID and BadgeID to 0

What do you mean by switching it around?

If you scroll up a bit, I sent a modified version of the original code.

1 Worked. The badgeID works alone but not the gamepassID

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

local badgeid = 155440073325875
local gamepassid = 1014470325

local function playerGates(player)
	local Character = player.Character

	local Success, HasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeid)
	end)

	local Success2, HasGamepass = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassid)
	end)

	if not HasBadge or not HasGamepass then
		local gateScript = game.Workspace:FindFirstChild("Gate2") and game.Workspace.Gate2:FindFirstChild("TouchGateScript")
		if gateScript then
			gateScript.Enabled = false
		end
	end
end

local event = game.ReplicatedStorage.JoinEvent

event.OnServerEvent:Connect(function(player)
	playerGates(player)
end)

Yeah just check if they have the gamepass aswell. Here’s the modified code to check for the gamepass.

1 Like

The script still does not work. It only does the badge and not the gamepass. I have the gamepass id in the game and do not have the badge in the game.

1 Like

Whoops yeah my bad, give me a minute to edit the original code. I made a typo lol.

1 Like

Alright updated it, try again.