Hello, So i’m having trouble with creating a paid access vehicle. I’ve tried numerous scripts but haven’t worked out the way i intended. My aim was to create a script which would kill and prompt you to purchase the gamepass if you sat down on the driver seat not owning the pass.
Here, I made a script that will do that. All you have to do is set gamepassId to your gamepassID and put the script into the vehicle seat. I made lots of comments so you can see how it works
local MarketPlaceservice = game:GetService("MarketplaceService") -- Get's MarketPlace service
local gamepassId = Your_Game_Pass_Id_Here
script.Parent.Changed:Connect(function() -- Fires when the seats occunpant changes (Someone sits in it)
local plr -- player variable stores the player
if script.Parent.Occupant then -- Checks to make sure the occupants character exists so we don't get an error
plr = game.Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent) -- Gets the player from the character using the humanoid that sat in the seat and sets the variable to it
end
if plr then -- makes sure the player was found by the above scripts
if MarketPlaceservice:UserOwnsGamePassAsync(plr.UserId,gamepassId) then -- Checks if the player owns the gamepass
--if they do, do nothing let them sit
else -- If they dont this script will fire
if plr.Character then -- Checks for the character so we don't get an error
plr.Character.Humanoid.Health = 0 -- Sets the players health to 0, killing them
end
end
end
end)
local marketplace = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local seat = script.Parent
local seatEvent = seat:GetPropertyChangedSignal("Occupant")
local gamepassId = 0 --Change to ID of gamepass.
local function onSeatOccupantChanged()
if seat.Occupant then
local humanoid = seat.Occupant
local character = humanoid.Parent
local player = players:GetPlayerFromCharacter(character)
if player then
local success, result = pcall(function()
return marketplace:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if success then
if result then
print(result)
return
end
else
warn(result)
end
success, result = pcall(function()
return marketplace:PromptGamePassPurchase(player, gamepassId)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
local seatWeld = seat:FindFirstChild("SeatWeld")
if seatWeld then
seatWeld:Destroy()
end
end
end
end
seatEvent:Connect(onSeatOccupantChanged)
The script previously provided would have killed the player’s character if they didn’t own the required gamepass, this script will simply remove the player’s character from the seat instead, this implementation will also prompt the gamepass to be purchased (if it is not already owned).