hello, I currently have low script knowledge and would like to get help with this.
so basically I’d need a textbutton that is unclickable (or locked) under a gamepass (some sort of a pay wall, I need this for my rpg game, since it’s a mount gamepass (horse). if the user owns the gamepass, the textbutton will be clickable (unlocked)
thanks in advance!
3 Likes
--Local Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local GAME_PASS_ID = 12345678 -- Replace with your Game Pass ID
local player = Players.LocalPlayer
local textButton = script.Parent
local cooldown = false -- Tracks whether the button is on cooldown
-- Function to check game pass ownership
local function checkGamePassOwnership()
local success, hasPass = pcall(function()
return MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAME_PASS_ID)
end)
if success and hasPass then
textButton.Text = "Ride Horse" -- Update button text
textButton.Active = true -- Enable the button
textButton.AutoButtonColor = true
else
textButton.Text = "Locked - Buy Mount Game Pass"
textButton.Active = false -- Disable the button
textButton.AutoButtonColor = false
end
end
-- Initial check
checkGamePassOwnership()
-- Handle button clicks with cooldown
textButton.MouseButton1Click:Connect(function()
if cooldown then return end -- Ignore clicks during cooldown
if not textButton.Active then
cooldown = true -- Set cooldown
MarketplaceService:PromptGamePassPurchase(player, GAME_PASS_ID)
-- Set a short cooldown (e.g., 3 seconds)
task.delay(3, function()
cooldown = false
end)
end
end)
-- Server Script
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GAME_PASS_ID = 12345678 -- Replace with your Game Pass ID
local spawnEvent = ReplicatedStorage:WaitForChild("SpawnHorseEvent")
local playerCooldowns = {} -- Tracks cooldowns for each player
-- Validate game pass ownership on server
spawnEvent.OnServerEvent:Connect(function(player)
-- Check cooldown
if playerCooldowns[player] then
print(player.Name .. " is on cooldown. Ignoring request.")
return
end
-- Set cooldown
playerCooldowns[player] = true
task.delay(3, function() -- Set a 3-second cooldown
playerCooldowns[player] = nil
end)
local success, hasPass = pcall(function()
return MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAME_PASS_ID)
end)
if success and hasPass then
-- Allow horse spawning or grant access
print(player.Name .. " owns the game pass. Granting access.")
else
-- Deny access
print(player.Name .. " does not own the game pass.")
player:Kick("Unauthorized access attempt.")
end
end)
2 Likes
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.