How to make script run if you own a gamepass

I’m trying to make this script only run if you own a certain gamepass but everytime i add the logic of only running if it owns a gamepass and don’t run at all if you don’t it always seems to bug like an animation not working or something so this is an old version of the script because the new version is broken but how would i go off running only if you own an gamepass and if you dont essentialy disable the script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local ClientModules = ReplicatedStorage.Modules
local BigNum = require(ClientModules.BigNum)
local Helpers = require(ServerStorage.Modules:WaitForChild("HelperFunctions"))

local StrengthTool = game.StarterPack:WaitForChild("Punch") -- Tool for Strength
local EnduranceTool = game.StarterPack:WaitForChild("PushUp") -- Tool for Endurance

local autoTrainingActive = {} -- Tracks if a player is auto-training
local GamePassOwners = {}  -- To track GameP ass ownership
local toolEquipDelays = {} -- To track delays when a tool is equipped

local EQUIP_DELAY_TIME = 1 -- Delay time in seconds before starting training when tool is equipped

-- Stops auto-training
local function stopTraining(player)
	autoTrainingActive[player] = false
end

-- Prevents tool usage during auto-training
local function onToolActivated(player, tool)
	if autoTrainingActive[player] then
		warn("Manual tool usage is disabled during auto-training.")
		return
	end

	-- Manual training if auto-training is not active
	if tool.Name == StrengthTool.Name then
		Helpers.Train(player, "Strength")
	elseif tool.Name == EnduranceTool.Name then
		Helpers.Train(player, "Endurance")
	end
end

-- Starts auto-training with a delay after tool equip
local function startAutoTraining(player, statName, tool)
	-- Check if the player owns the GamePass
	if GamePassOwners[player.UserId] then
		-- If they own the GamePass, disable auto-training if they are in a zone with the tool equipped
		local _, InEnduZone = Helpers.IsInUsableZone(player, "Endurance")
		if InEnduZone then
			autoTrainingActive[player] = false
			return  -- Stop the auto-training in the zone if the player has the GamePass
		end
	end

	-- Check if the tool is already equipped and a delay is active
	if toolEquipDelays[player.UserId] and tick() - toolEquipDelays[player.UserId] < EQUIP_DELAY_TIME then
		return -- Prevent starting auto-training again if delay is still active
	end

	-- Set the delay timer when the tool is equipped
	toolEquipDelays[player.UserId] = tick()

	autoTrainingActive[player] = true
	local stat = player:FindFirstChild("PhysicalStats"):FindFirstChild(statName)
	local Multi = player:FindFirstChild("Multipliers"):FindFirstChild(statName .. "Multi")
	local trainingMultiplier = 1

	-- Only apply zone multiplier if not in Endurance zone
	local _, InEnduZone = Helpers.IsInUsableZone(player, "Endurance")
	if InEnduZone then
		trainingMultiplier = 1 -- Prevent zone multiplier for Endurance zone
	else
		-- Apply regular multipliers
		if Multi then
			trainingMultiplier = trainingMultiplier * tonumber(Multi.Value)
		end
	end

	if not stat then
		warn("Stat not found for player: " .. statName)
		return
	end

	-- Animation setup
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	local Animator = Humanoid:WaitForChild("Animator")

	local Punch1Anim = Instance.new("Animation")
	Punch1Anim.AnimationId = "rbxassetid://18963113696"
	local Punch1Track = Animator:LoadAnimation(Punch1Anim)
	Punch1Track.Priority = Enum.AnimationPriority.Action
	Punch1Track.Looped = false

	local Punch2Anim = Instance.new("Animation")
	Punch2Anim.AnimationId = "rbxassetid://18963038341"
	local Punch2Track = Animator:LoadAnimation(Punch2Anim)
	Punch2Track.Priority = Enum.AnimationPriority.Action
	Punch2Track.Looped = false

	local PushUpAnim = Instance.new("Animation")
	PushUpAnim.AnimationId = "rbxassetid://18963052398"
	local PushUpTrack = Animator:LoadAnimation(PushUpAnim)
	PushUpTrack.Priority = Enum.AnimationPriority.Action2
	PushUpTrack.Looped = false

	local PushUpIdleAnim = Instance.new("Animation")
	PushUpIdleAnim.AnimationId = "rbxassetid://18953230780"
	local PushUpIdleTrack = Animator:LoadAnimation(PushUpIdleAnim)
	PushUpIdleTrack.Priority = Enum.AnimationPriority.Action2
	PushUpIdleTrack.Looped = true

	local AnimToPlayNext = Punch1Track

	-- Alternates punch animations
	local function playNextPunchAnimation()
		if AnimToPlayNext == Punch1Track then
			Punch1Track:Play()
			AnimToPlayNext = Punch2Track
		else
			Punch2Track:Play()
			AnimToPlayNext = Punch1Track
		end
	end

	-- Auto-training loop
	while autoTrainingActive[player] and Character and Character:FindFirstChild(tool.Name) do
		if tool.Name == "Punch" then
			playNextPunchAnimation()
		elseif tool.Name == "PushUp" then
			if not PushUpIdleTrack.IsPlaying then
				PushUpIdleTrack:Play()
			end
			PushUpTrack:Play()
		end

		-- Simulates training delay
		task.wait(1.125)

		-- Gain stats with the correct multiplier
		if autoTrainingActive[player] then
			Helpers.Train(player, statName, trainingMultiplier) -- Apply multiplier in training
		end
	end

	-- Stops all animations
	Punch1Track:Stop()
	Punch2Track:Stop()
	PushUpTrack:Stop()
	PushUpIdleTrack:Stop()
end

-- Main connections
Players.PlayerAdded:Connect(function(player)
	-- Check GamePass ownership when the player joins
	local success, hasGamePass = pcall(function()
		return player:HasGamePass(951288782)  -- Replace with your GamePass ID
	end)

	if success then
		GamePassOwners[player.UserId] = hasGamePass
	end

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		-- Stops auto-training and unequips tools if the player dies
		humanoid.Died:Connect(function()
			stopTraining(player)
			humanoid:UnequipTools()
		end)

		-- Detects tool usage
		character.ChildAdded:Connect(function(tool)
			if tool:IsA("Tool") then
				tool.Activated:Connect(function()
					onToolActivated(player, tool)
				end)

				-- Starts auto-training when relevant tool is equipped
				if tool.Name == StrengthTool.Name then
					startAutoTraining(player, "Strength", tool)
				elseif tool.Name == EnduranceTool.Name then
					startAutoTraining(player, "Endurance", tool)
				end
			end
		end)
	end)

	-- Stops training on character reset
	player.CharacterRemoving:Connect(function()
		stopTraining(player)
	end)

	-- Clears auto-training data when player leaves
	player.AncestryChanged:Connect(function()
		if not player:IsDescendantOf(Players) then
			stopTraining(player)
		end
	end)
end)```
1 Like

Use marketplace service

UserOwnsGamePassAsync

local function Function(plrID, gamepassid)
if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamepassid) then 

-- code

end
end

Disabling the script won’t work cuz it should be disabled for everyone then cuz it’s a server script

You need to call this from the service itself; the function is not a built-in. You’ll also want to package the if-statement’s body into a function so that you may re-use the code when the game-pass is purchased. This ensures users do not have to rejoin the game for the game-pass’ goods/services to activate