Enable and disable scripts depending on if player has a gamepass

hi, I was looking at some posts but they weren’t very helpful on how to disable and enable a script when having a gamepass, I don’t have a lot of programming experience.
I have various scripts that I want to enable and disable in StarterCharacterScripts.

local MPS = game:GetService("MarketplaceService")
local GamePassID = 1234567890	-- Change to the correct ID
local ForGamepassUsersOnly = game:GetService("ReplicatedStorage").forGamepassUsersOnly	-- Use folder for easier use and modularity

game:GetService("Players").PlayerAdded:Connect(function(player: Player) 
	local result = MPS:UserOwnsGamePassAsync(player.UserId, GamePassID)
	
	if result then
		print(player.Name .. " owns the gamepass with ID: " .. GamePassID)
		for i, v in pairs(ForGamepassUsersOnly:GetChildren()) do
			if v:IsA("Script") or v:IsA("LocalScript") or v:IsA("ModuleScript") then	-- Unnecessary, but if you have other stuff inside saem folder, then it's good to have
				local clone = v:Clone()
				clone.Parent = player.PlayerScripts
				clone.Enabled = true
			end
		end
	else
		print(player.Name .. " does not own the gamepass with ID: " .. GamePassID)
		return
	end
end)

Here’s basic script. I didn’t have any owned gamepasses on hands so I couldn’t test the functionality, but this should work. Remember to change gamepassID to desired ID and move all wanted scripts in folder for modularity and easier use in future.

ADDITIONAL INFORMATION (I forgot to add):

  1. Put this script in ServerScriptService as Script
  2. Create folder in ReplicatedStorage and put all scripts you want inside that folder (easier management and modularity)
  3. That’s it. If you need additional help, reply to this post.

(PS. This is free to use script, made by me. No need to credit.)

1 Like

Well if it’s a local script a exploiter can enable it

Yes, I added additional information bit lower in the post.
It is important to perform these kind of tasks on server-side.

But if it’s a local script can they toggle it enabled or not just on the client

Client cannot access the scripts in-game. Only way for user to access scripts is to use exploiting client (modified Roblox client) and even after using that, they can only access local scripts, ran in player, since serverScriptService is located in Roblox Servers, unaccessible to players.

I modified a few things to use the serverscriptservice

but I am curious how to prevent the use of hacks

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