i want to have a gamepass that allows you to press a button and it will take a part from server storage and place it next to the player who pressed the keybind. The keybind should only work if the user has the gamepass. I also want a sound to play when the part is spawned. I also want there to be a cooldown, and I want these parts to be removed after a certain period of time.
to do this, you will need to use some roblox services
if you want this part to spawn server-side, input detection will have to be ran on the client and part spawning on the server, to do this we will also have to use a remote event
this remoteevent will be called "SpawnPartUsingGamepass" and will be put in ReplicatedStorage
our server script will be put in ServerScriptService and will have the following code:
-- load the services local MarketPlaceService = game:GetService("MarketPlaceService") local ReplicatedStorage = game:GetService("ReplicatedStorage") -- our gamepass id local SpawnPartGamepassId = 123456789 -- function to check if a player owns the gamepass id local function CheckPlayerGamepassStatus(Player) -- create.roblox.com/docs/reference/engine/classes/MarketplaceService#UserOwnsGamePassAsync return MarketPlaceService:UserOwnsGamePassAsync(SpawnPartGamepaddId) end -- event listener ReplicatedStorage:WaitForChild("SpawnPartUsingGamepass").OnServerEvent:Connect(function(Player) if not CheckPlayerGamepassStatus(Player) then return -- end the event listener end -- your code to spawn a part goes below this comment end()
our client script will then be put in StarterPlayerScripts with the following content:
-- load the services local UserInputService = game:GetService("UserInputService ") local MarketPlaceService = game:GetService("MarketPlaceService") local ReplicatedStorage = game:GetService("ReplicatedStorage") -- the remote event local SpawnPartEvent = ReplicatedStorage:WaitForChild("SpawnPartUsingGamepass") -- button keycode -- https://create.roblox.com/docs/reference/engine/enums/KeyCode local SpawnKeycode = Enum.Keycode.R -- our gamepass id local SpawnPartGamepassId = 123456789 -- check if the player owns the gamepass so the event won't be fired by non-gamepass holding players if not MarketPlaceService:UserOwnsGamePassAsync(SpawnPartGamepaddId) then -- the player does not own the gamepass, destroy the script script:Destroy() end -- listen for any input -- https://create.roblox.com/docs/reference/engine/classes/UserInputService#InputBegan UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent) if GameProcessedEvent then -- ignore the request if roblox has handled the input return end if Input.KeyCode == SpawnKeycode then SpawnPartEvent:FireServer() end end
the reason we check if the player owns the gamepass both on the client and server is because
respectively.
hopefully this helps in answering your question, if you have any questions for me, feel free to leave a comment under here!
have a good day :D
p.s. if you're going to copy any code here, remember to press view source to copy the entire code, some of it might be cut off.