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

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

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

✓ Accepted answer

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!

1 comment

0

Thank you for your help! <3

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

1 comment

0

Thank you for clearing that up for me, I'm rather unknowledgable when it comes to where local scripts work.

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