Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I make a gamepass that lets you use a keybind that spawns a part?

Asked by 1 year ago

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.

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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

  • the client won't fire events when the client doesn't have to, saving on bandwith and a bit of computing power
  • the server won't spawn a part when a hacker (who doesn't own the gamepass) attempts to spawn one in

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.

0
I'm not sure how to code it so that it actually spawns the part I want. I have the part in serverstorage and I'm not sure what to do with the part itself. LunaticFETCH 5 — 1y
0
in the server script, you can add the serverstorage service at the top where the other services are, declare the part as a variable and clone it, then parent it. cloning and parenting would be placed after the spawn comment. loowa_yawn 383 — 1y
0
i'm confused LunaticFETCH 5 — 1y
Ad

Answer this question