To transfer this over to a local script, you will first need to be able to reference the local player that the local script belongs to:
local Player = game.Players.LocalPlayer
Then you just do what you're doing in the PlayerAdded function inside the local script. Here's what your code should look like:
local MarketPlaceService = game:GetService("MarketplaceService")
local VipParts = {game.Workspace:WaitForChild("VIPEntrance"), game.Workspace:WaitForChild("VIPEntrance2"), game.Workspace:WaitForChild("VipAcess")}
local TeleportPart = game.Workspace.PartToTeleport
local gamePassId = 8757181
local Player = game.Players.LocalPlayer
local HasPass = false
local sucess, errormessage = pcall(function()
HasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, gamePassId)
end)
if HasPass == true then
for i, v in pairs(VipParts) do
v:Destroy()
end
end
However this would only break the VipParts if the player joins the game with the VIP gamepass already bought, it won't destroy if the player buys the VIP gamepass while in-game. You will have to use the MarketplaceService.PromptGamePassPurchaseFinished event to detect when a player successfully purchases a gamepass.
Here's the wiki link on the event: https://developer.roblox.com/en-us/api-reference/event/MarketplaceService/PromptGamePassPurchaseFinished
Here's what your code should look like:
local MarketPlaceService = game:GetService("MarketplaceService")
local VipParts = {game.Workspace:WaitForChild("VIPEntrance"), game.Workspace:WaitForChild("VIPEntrance2"), game.Workspace:WaitForChild("VipAcess")}
local TeleportPart = game.Workspace.PartToTeleport
local gamePassId = 8757181
local Player = game.Players.LocalPlayer
local HasPass = false
local sucess, errormessage = pcall(function()
HasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, gamePassId)
end)
if HasPass == true then
for i, v in pairs(VipParts) do
v:Destroy()
end
end
MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(plr, id, purchased)
if plr == Player and id == gamePassId and purchased then
for i, v in pairs(VipParts) do
v:Destroy()
end
end
end)
We can further improve this code by using functions so that we don't have to repeat code, specifically the code that destroys vip parts.
local function DestroyVipParts()
for i, v in pairs(VipParts) do
v:Destroy()
end
end
Here's how your code should look like:
local MarketPlaceService = game:GetService("MarketplaceService")
local VipParts = {game.Workspace:WaitForChild("VIPEntrance"), game.Workspace:WaitForChild("VIPEntrance2"), game.Workspace:WaitForChild("VipAcess")}
local TeleportPart = game.Workspace.PartToTeleport
local gamePassId = 8757181
local Player = game.Players.LocalPlayer
local HasPass = false
local function DestroyVipParts()
for i, v in pairs(VipParts) do
v:Destroy()
end
end
local sucess, errormessage = pcall(function()
HasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, gamePassId)
end)
if HasPass then
DestroyVipParts()
end
MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(plr, id, purchased)
if plr == Player and id == gamePassId and purchased then
DestroyVipParts()
end
end)
1 comment
i want to have it local.