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

I have a question. How can I make a part that only certain other parts can collide with?

Asked by
nc2r 117
4 years ago

I want to make a shop that only certain players can enter. I know how to retrieve the data, but how can I make a part that is completely uncollideable to some players but can collide with others, thus blocking them?

1
You could use a touched event so when a player touches the door you can then compare this to the data you have and then cancollide with the door as necessary. Greenhands1 4 — 4y
1
Or if you want it so other players can't go through the door when an allowed player touches it you can use the touched event to then teleport the humanoidrootpart in the player to the other side. Greenhands1 4 — 4y
0
You can use PhysicsService check my answer! It's at the bottom :/ maumaumaumaumaumua 628 — 4y

3 answers

Log in to vote
2
Answered by 4 years ago

You can do this by using PhysicsService. To do what you're trying to do, first you have to make the Collision group.

local physicsService=game:GetService("PhysicsService")
local name = "parts" --// put this to whatever you want
local collisionGroup = physicsService:CreateCollisionGroup(name);

Now that you have created it we have to set the part's collision group

local part = yourpart --// you have to set your part
physicsService:SetPartCollisionGroup(part,name) --// the name

Perfect! Now we have setup the parts collision group, now we'll add the players collision group so we can set them non collidable

local pname = "players" --// you can also change this
physicsService:CreateCollisionGroup(pname)

Now that we have the groups we'll start with a player added event so we can set the parts

local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player) --// listens to every new player that joins
player.CharacterAdded:Connect(function(character) --// listens to character added
--// now here we add the parts to the collision group
for i,v in pairs(character:GetDescendants()) do
if v:IsA("BasePart") then
physicsService:SetPartCollisionGroup(v,pname) --// basically what this does is add all the player's parts to the collision group
end
end
--// now we'll listen for any part created
character.DescendantAdded:Connect(function(descendant)
if descendant:IsA("BasePart") then
physicsService:SetPartCollisionGroup(descendant,pname)
end
end)

end)
end)

That's it! Now we just have to set them to not collide!

physicsService:CollisionGroupSetCollidable(name,pname,false) --// sets collission off

Done! You have created it! Now you just have to determine if the player has the permission to enter!

Final Code:

local part = yourpart --// you have to set your part
physicsService:SetPartCollisionGroup(part,name) --// the name

local pname = "players" --// you can also change this
physicsService:CreateCollisionGroup(pname)

local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player) --// listens to every new player that joins
player.CharacterAdded:Connect(function(character) --// listens to character added
--// now here we add the parts to the collision group
for i,v in pairs(character:GetDescendants()) do
if v:IsA("BasePart") then
physicsService:SetPartCollisionGroup(v,pname) --// basically what this does is add all the player's parts to the collision group
end
end
--// now we'll listen for any part created
character.DescendantAdded:Connect(function(descendant)
if descendant:IsA("BasePart") then
physicsService:SetPartCollisionGroup(descendant,pname)
end
end)

end)
end)

physicsService:CollisionGroupSetCollidable(name,pname,false) --// sets collission off

Hope this helped :D if it did please accept it!

Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

step one: make local script

step two: get the player in that local script (if you doing something like checking for a gamepass

step two and a half: get the part you want the player to go through

step three: make an if statement. set your criteria into the if statement.

step four: in the if statement, make the part that the player is going

the script should look like:

local player=game.Players.LocalPlayer
local door=set_the_door_here

if set_your_criteria_here then
    door.CanCollide=true
else
    door.CanCollide=false
end

(this is a simple template that you can fill in the set_the_door_here and set_your_criteria_here. none of this will replicate to other players because it is made in a localscript.)

Log in to vote
0
Answered by
nc2r 117
4 years ago
Edited 4 years ago

FireClient() all of the clients that meet the requirement from the server. Then, in a localscript, make the part CanCollide off in an OnClientEvent, so this change will only be replicated to the clients who meet the requirements.

Answer this question