Changing a players collision group if they are sitting

I want to create a script that changes the players collision group if they’re sitting down in a seat.

When the player is sat, they remain in the collision group “Players”, when I want them moved to “PlayersSat”.

This is the script that detects the player and sets them to the "Players" collision group. (THIS ONE WORKS)
local ps=game:GetService("PhysicsService")

game.Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(char)
          wait()
          for _,v in pairs(char:GetChildren()) do
               if v:IsA("BasePart") then
                    ps:SetPartCollisionGroup(v,"Players")
               end
          end
     end)
end)

However, the script that is meant to change the players collision group if sat, doesn’t work.

This is the script that is meant to detect if the player is sat and set them to the "PlayersSat collision group. (THIS ONE DOESN'T WORK)
local ps=game:GetService("PhysicsService")

Player = plr.CharacterAdded:Connect(function(char) --Get the player's character here
if Player.Humanoid.Sit == true then
          for _,v in pairs(char:GetChildren()) do
               if v:IsA("BasePart") then
                    ps:SetPartCollisionGroup(v,"PlayersSat")
               end
          end
     end
end

All help will be greatly appreciated. :slight_smile:

2 Likes
local ps=game:GetService("PhysicsService")
plr.CharacterAdded:Connect(function(char) --Get the player's character here
    if char.Humanoid.Sit == true then
          for _,v in pairs(char:GetChildren()) do
               if v:IsA("BasePart") then
                    ps:SetPartCollisionGroup(v,"PlayersSat")
               end
          end
     end
end)

You’re trying to retrieve the Character from a function (which is hooked to an event). That’s not how it works. You also forgot to close the parenthesis for the Connect() function, which is why I added a bracket to line 11. However, that code above I posted will not work! Why? Because, it’s highly doubtful that when the character is added that they would be in the seat state. So, what can we do to fix this? GetPropertyChangedSignal.

local ps=game:GetService("PhysicsService")
plr.CharacterAdded:Connect(function(char) --Get the player's character here
    char.Humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
          for _,v in pairs(char:GetChildren()) do
               if v:IsA("BasePart") then
                   if char.Humanoid.Sit then
                       ps:SetPartCollisionGroup(v,"PlayersSat")
                   else
                       ps:SetPartCollisionGroup(v,"Players")
                   end
               end
          end
     end)
end)

Now, the code will detect if the Sit property of the Humanoid has changed. The code will run through the children under the character object and will detect if Sit is true or not. If it is, it’ll set the child’s collision group to PlayersSat. If not, it’ll set the child’s collision group to “Players”.

Also, I’m assuming you have the variable plr stored somewhere else in your code. Otherwise, you’ll need to add this:

game.Players.PlayerAdded:Connect(function(plr)

end)

Put the existing code into that function and it should work just fine. Also, I do not recommend referencing a service directly in code without it being a pre-existing variable, nor do I recommend acquiring a service the way I put it. I would put it as game:GetService("Players"), for example.

4 Likes

Thank you so much! You helped me a lot. :grin:

1 Like