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

Why does my code not work when a player touches the part?

Asked by 4 years ago

Here's my problem.

I have a part named BARISTADOOR, and a Local Script named BARISTADOORSCRIPT.

(The Local Script is in the part.)

This is the code that is in the Local Script.

game.Workspace.BARISTADOOR.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
       if player:GetRankInGroup(5370359) >= 12 then
       game.Workspace.BARISTADOOR.CanCollide = false
       end
    end
end)

Whenever a player touches the part, I need it to do,

if player:GetRankInGroup(5370359) >= 12 then
game.Workspace.BARISTADOOR.CanCollide = false`
end

Only for the client that touched te part.

But it's not doing anything when I touch it.

Can someone explain why this does not work?

Thank you.

2 answers

Log in to vote
1
Answered by 4 years ago

An easy way to do this on the server side is to use PhysicsService and collision groups. This would allow you to set the door and players in a collision group so they don't collide.

Server Script in ServerScriptService

local physicsService = game:GetService("PhysicsService");
local door = workspace:FindFirstChild("BARISTADOOR");
local groupId, minRank = 5370359, 12;


physicsService:CreateCollisionGroup("Barista"); --Create collision group
physicsService:CollisionGroupSetCollidable("Barista", "Barista", false); --Set collision group to not collide with itself
physicsService:SetPartCollisionGroup(door, "Barista"); --Add door to collision group

game:GetService("Players").PlayerAdded:Connect(function(player)
  if player:GetRankInGroup(groupId) >= minRank then --Check the players rank when they join
    local function setGroup(character)
      for _, obj in ipairs(character:GetDescendants()) do
        if obj:IsA("BasePart") then --Check if object is a part
          physicsService:SetPartCollisionGroup(part, "Barista"); --Add the part to the collision group
        end;
    end;
    player.CharacterAdded:Connect(setGroup); --Run this each time the character is added
  end;
end);

If this solved your question please like this and mark it as the solution so people know it has been answered. If you have any other questions feel free to ask!

I hope this helps!

0
Thank you for your help! <3 Sloth_WasTaken 41 — 4y
Ad
Log in to vote
1
Answered by
Lazarix9 245 Moderation Voter
4 years ago

Local scripts don't run in workspace, just move it to somewhere like StarterPlayerScripts and it's gonna be fine

0
Thank you for clearing that up for me, I'm rather unknowledgable when it comes to where local scripts work. Sloth_WasTaken 41 — 4y

Answer this question