If player has Premium then they can go through a part

I have a fairly simple script where if a player has premium they can go through a part else they cannot.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
if player.MembershipType == Enum.MembershipType.Premium then
	script.Parent.CanCollide = false
else
	script.Parent.CanCollide = true
end

Why is this not working?

1 Like

Is the script a local or serverscript?

Is this program written in a LocalScript object, or is it written in a Script object? It must be placed in the LocalScript object for it to work. Also, this script must be inside of the part.

2 Likes

This is a normal script within the part.

The MembershipType property only works inside a LocalScript. Try copy and pasting inside a LocalScript.

1 Like

If it’s a server script, you can’t get the player by doing LocalPlayer. Besides, you should make this a local script anyway, because if you don’t then the part will have CanCollide false for everyone.

2 Likes

Future reference, this is wrong. It can be accessed from both.

1 Like

you need to make it a local script. you can’t call localplayer over a normal server script. Also you can change the part properties through a local script if you are scared it won’t change through the client.

Ah, my bad. Read the devwiki wrong

I would recommend doing this with a server script, as clients can change their membership status with exploits if they really wanted to.

Code taken from the wiki tutorial

local PhysicsService = game:GetService("PhysicsService")
local premiumDoors = "PremiumDoors"
 
-- Create door collision groups
PhysicsService:CreateCollisionGroup(premiumDoors)
PhysicsService:SetPartCollisionGroup(workspace.PremiumDoor, premiumDoors)

local premiumPlayers = "PremiumPlayers"
-- Create player collision groups
PhysicsService:CreateCollisionGroup(premiumPlayers)

local function setCollisionGroup(character, groupName)
	for _, child in ipairs(character:GetChildren()) do
		if child:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(child, groupName)
		end
	end
	character.DescendantAdded:Connect(function(descendant)
		if descendant:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(descendant, groupName)
		end
	end)
end

local Players = game:GetService("Players")
 
local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		if player.MembershipType == Enum.MembershipType.Premium then
			setCollisionGroup(character, premiumPlayers)
		end
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)

PhysicsService:CollisionGroupSetCollidable(premiumDoors, premiumPlayers, false)
2 Likes