A classic Roblox script! Here is a fully working script that does exactly what you asked for:
-- Configuration
local part = script.Parent -- The part that players will touch
local gamepassId = 123456789 -- Replace with your gamepass ID
local itemName = "Cool Sword" -- Name of the item to give
local itemId = 987654321 -- ID of the item to give (must be a valid Roblox asset)
-- Services
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
-- Function to check if player owns gamepass
local function playerOwnsGamepass(player)
local hasGamepass = false
local success, result = pcall(function()
hasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if not success then
warn("Error checking gamepass ownership for player " .. player.Name .. ": " .. tostring(result))
end
return hasGamepass
end
-- Function to give item to player
local function giveItem(player)
local character = player.Character
if character then
local backpack = player.Backpack
local tool = game.ServerStorage:FindFirstChild(itemName)
if tool then
local toolClone = tool:Clone()
toolClone.Parent = backpack
print(player.Name .. " received " .. itemName)
else
warn("Item '" .. itemName .. "' not found in ServerStorage!")
end
else
warn("Player " .. player.Name .. " has no character!")
end
end
-- Function to prompt gamepass purchase
local function promptPurchase(player)
MarketplaceService:PromptGamePassPurchase(player, gamepassId)
end
-- Connect Touched event to part
part.Touched:Connect(function(hit)
-- Check if the object that touched the part is a player's character part
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
-- Check if player owns gamepass
if playerOwnsGamepass(player) then
-- Player owns gamepass, give item
giveItem(player)
else
-- Player doesn't own gamepass, prompt purchase
promptPurchase(player)
end
end
end)
Here's how the script works:
- Configuration: Set the
gamepassId, itemName, and itemId variables to your desired values.
- The script uses two Roblox services:
MarketplaceService (for gamepass checks and purchases) and Players (to get the player from a character).
playerOwnsGamepass function: Checks if a player owns the specified gamepass using MarketplaceService:UserOwnsGamePassAsync. If the check fails, it prints a warning.
giveItem function: If the player owns the gamepass, this function clones the item (a Tool instance) from ServerStorage and parents it to the player's Backpack.
promptPurchase function: If the player doesn't own the gamepass, this function opens the gamepass purchase prompt using MarketplaceService:PromptGamePassPurchase.
Touched event: When the part is touched, the script checks if the object that touched it is a player's character part. If so, it gets the corresponding Player instance.
- The script then checks if the player owns the gamepass using
playerOwnsGamepass. Depending on the result:
- If true, it gives the item using
giveItem.
- If false, it prompts the gamepass purchase using
promptPurchase.
Important requirements:
- Place this script inside the part that players will touch (e.g., a
BasePart like Part, MeshPart, etc.).
- Create a
Tool instance with the desired item name (e.g., "Cool Sword") inside ServerStorage. This will be the item given to players who own the gamepass.
- Replace
gamepassId and itemId with your actual gamepass and item IDs (you can find these in the Roblox Developer website).
- Make sure your gamepass is properly configured and priced in the Roblox Developer website.
Test the script by:
- Touching the part without owning the gamepass (should prompt purchase).
- Buying the gamepass and touching the part again (should receive the item).
That's it! If you have any questions or issues, feel free to ask. Happy developing! 🎉