make me a roblox script that detect when a player touch a part , then it verify if…
- User
make me a roblox script that detect when a player touch a part ,
then it verify if player have a gamepass, if he have it, it give the item, if not it prompt the purchase
Assistant
Certainly! Below is a Roblox script that detects when a player touches a part. It then verifies if the player has a specific GamePass. If they do, it gives them an item. If they don't, it prompts them to purchase the GamePass.
Place this script inside a ServerScript within the part that players will touch.
-- Configuration local gamePassId = 123456 -- Replace with your GamePass ID local itemToGive = "ItemName" -- Replace with the name of the item to give -- Services local MarketplaceService = game:GetService("MarketplaceService") local Players = game:GetService("Players") -- Function to give the item to the player local function giveItem(player) -- Assuming the item is in ReplicatedStorage, adjust as needed local item = game.ServerStorage:FindFirstChild(itemToGive) if item then local clonedItem = item:Clone() -- Assuming the item should go into the player's backpack, adjust as needed clonedItem.Parent = player.Backpack print(player.Name .. " has been given " .. itemToGive) else warn("Item " .. itemToGive .. " not found in ServerStorage!") end end -- Function to prompt the player to buy the GamePass local function promptGamePassPurchase(player) -- Prompt the player to buy the GamePass MarketplaceService:PromptGamePassPurchase(player, gamePassId) end -- Touch event script.Parent.Touched:Connect(function(hit) local character = hit.Parent local player = Players:GetPlayerFromCharacter(character) -- Check if the player is valid if player then -- Verify if the player owns the GamePass local hasGamePass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId) if hasGamePass then -- Player has the GamePass, give them the item giveItem(player) else -- Player does not have the GamePass, prompt them to purchase it promptGamePassPurchase(player) end end end)Explanation:
-
Configuration:
gamePassId: Replace123456with your actual GamePass ID.itemToGive: Replace"ItemName"with the name of the item you want to give to the player. Ensure this item is located inServerStorageor adjust the script accordingly.
-
Services:
MarketplaceService: Used to check if a player owns a GamePass and to prompt the purchase.Players: Used to get the player instance from the character.
-
giveItem Function:
- This function clones the item from
ServerStorageand parents it to the player's backpack. Adjust the location as needed.
- This function clones the item from
-
promptGamePassPurchase Function:
- This function prompts the player to purchase the game pass using MarketplaceService.
-
Touched Event:
- The script listens for the
Touchedevent on the part. - When a part is touched it checks if the
hitobject is part of a valid character. - It then gets the player instance from the character.
- Using
MarketplaceService:UserOwnsGamePassAsync, it checks if the player owns the GamePass. - If the player owns the GamePass, it calls
giveItemto give the player the item. - If the player does not own the GamePass it calls
promptGamePassPurchaseto prompt the player to buy it.
- The script listens for the
Make sure the item you want to give is located in
ServerStorageor adjust the script to the correct location. Also, replace thegamePassIdwith your actual GamePass ID.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.