Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I'm making a VIP only room but it does not function correctly?

I'm making a VIP only room with a game-pass but the problem is:

It's on a server script but i want it to be on a Local Script.

I copied it on the Local Script but it did not work.. Does anyone know how to make it local?

local MarketPlaceService = game:GetService("MarketplaceService")
local VipParts = {game.Workspace.VIPEntrance, game.Workspace.VIPEntrance2, game.Workspace.VipAcess}

local TeleportPart = game.Workspace.PartToTeleport

local gamePassId = 8757181

game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(char)
			
		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
			end)
				end

Would be appreciated.

3 answers

✓ Accepted answer

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)

You had some issues with the ends at the bottom of your script which would cause errors, fixing those would work but there's some other things that might cause issues too, for example, you are immediately referencing things in the workspace which might cause issues if they aren't loaded yet. This can be fixed by using WaitForChild. You are also trying to delete the parts every time that the player spawns which might cause issues if the parts are already deleted and you try to delete them again when the player respawns.

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

game.Players.PlayerAdded:Connect(function(player)
    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
end)

1 comment

0

i want to have it local.

You would have to use remote events. At least some part of the code must be server side.

1 comment

0

I have not got any "Server side" script, so i cant create a Remote.

This discussion is preserved as it was. New answers, comments, and votes are closed.