Gamepass whitelist

lol, I saw that. Go get some sleep, you deserve a break. :slight_smile:

1 Like

This is good, thank you so much for the help! One last thing, do you mind if you can make it so that it’s already slocked once a server starts?

local ServerLock = true
local GamepassWhitelist = 0000001
local Whitelist = {
	{
		"Roblox",
		1
	},
	{
		"KaylaPls",
		nil
	},
}


game.Players.PlayerAdded:Connect(function(Player)
	if not ServerLock then return end
	local CanJoin = false
	for Index, WhitelistEntry in pairs(Whitelist) do
		if WhitelistEntry[1] ~= nil then
			if WhitelistEntry[1] == Player.Name then
				CanJoin = true
				break
			end
		end
		if WhitelistEntry[2] ~= nil then
			if WhitelistEntry[2] == Player.UserId then
				CanJoin = true
				break
			end
		end
	end
	if not CanJoin then
		local OwnsGamepass = false
		local success, err = pcall(function()
			OwnsGamepass = game:GetService("MarketplaceService"):UserOwnsGamepassAsync(Player.UserId, GamepassWhitelist)
		end)
		if success then
			if OwnsGamepass == true then
				CanJoin = true
			end
		end
	end
	if not CanJoin then
		Player:Kick("You are not whitelisted.")
	end
end)

Sure!

local Whitelisted = {["KaylaPls"] = true} 

local Gamepass = 123456
local serverlock = true

local Remote = game.ReplicatedStorage.SlockUnslock

Remote.OnServerEvent:Connect(function(Player,LockVal)
	wait()
	print(LockVal)
	if LockVal == "Locked" then
		serverlock = true
	end
	if LockVal == "Unlocked" then
		serverlock = false
	end
end)

game.Players.PlayerAdded:Connect(function(Player)
	if serverlock then
		if (not Whitelisted[Player.Name]) and (not game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, Gamepass)) then
			Player:Kick("This server is currently locked!")
		end
	end
end)

That puts together everything we have done above, and a slight change of having the serverlock variable a bool value instead of a dictionary table. (And of course making it locked at the beginning of the script)

After investigating your code, I have decided to slightly improve it, and go over a few things that’s been added and/or edited.

Firstly, you would need to check if they are either whitelisted or have the gamepass, in this case:

local function UserOwnsGamePassAsync(UserID)
	local Check = MarketplaceService:UserOwnsGamePassAsync(UserID, GamePassID)
	return Check -- Returns true if owned, false if not.
end

Now, checking it in our code:

if table.find(Whitelisted, Player.UserId) or UserOwnsGamePassAsync(Player.UserId, GamePassID) then

And for the final part: the code put together:


local Serverlock = false -- Default value. Set it to whatever you'd like.

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Remote = ReplicatedStorage.RemoteEventName
local Players = game:GetService('Players')
local MarketplaceService = game:GetService('MarketplaceService')
local GamePassID = 00000 -- Change it to your gamepass id.

local Whitelisted = {
	[00000]	= 'Username', -- Userid = Username
	[11111] = 'Username2'
}


local function OnServerEvent(Value)
	Serverlock = not Serverlock -- If true: returns false, if false: returns true.
end

local function UserOwnsGamePassAsync(UserID)
	local Check = MarketplaceService:UserOwnsGamePassAsync(UserID, GamePassID)
	return Check -- Returns true if owned, false if not.
end

local function PlayerAdded(Player)
	if Serverlock then
		if table.find(Whitelisted, Player.UserId) or UserOwnsGamePassAsync(Player.UserId, GamePassID) then
			-- They are whitelisted or owns the gamepass.
		else
			-- No whitelist, no gamepass. Do whatever you want here.
		end
	end
end


Remote.OnServerEvent:Connect(OnServerEvent) -- Connects the OnServerEvent function.

local GetPlayers = Players:GetPlayers()
for i = 1, #GetPlayers do
	local Player = GetPlayers[i]
	PlayerAdded(Player)
end
-- In case this script is added after a player is added, we loop through the current players that did not pass the event.

Players.PlayerAdded:Connect(PlayerAdded) -- Connects the PlayerAdded function.

Note - This is only an example, or pseudocode if you will. I have not tested this myself.

More information about pseudocode will be listed here.

Thank you so much! Also, how do I put multiple people in the whitelist table? When I try to seperate multiple usernames with commas and quotations, it gives me an error, breaks the script, and allows anyone to enter regardless if they are whitelisted or own the game pass, and regardless if serverlock is on.