BloXatoR

How to enable/disable player collison using Key in Roblox Studios.

Dec 16th, 2023
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | Gaming | 0 0
  1. local player = game.Players.LocalPlayer
  2.  
  3. -- Function to disable player collisions
  4. local function disableCollisions()
  5.     -- Check if the character and humanoid exist
  6.     if player.Character and player.Character:FindFirstChild("Humanoid") then
  7.         -- Disable collisions for each part in the character
  8.         for , part in pairs(player.Character:GetDescendants()) do
  9.             if part:IsA("BasePart") then
  10.                 part.CanCollide = false
  11.             end
  12.         end
  13.     end
  14. end
  15.  
  16. -- Function to enable player collisions
  17. local function enableCollisions()
  18.     -- Check if the character and humanoid exist
  19.     if player.Character and player.Character:FindFirstChild("Humanoid") then
  20.         -- Enable collisions for each part in the character
  21.         for , part in pairs(player.Character:GetDescendants()) do
  22.             if part:IsA("BasePart") then
  23.                 part.CanCollide = true
  24.             end
  25.         end
  26.     end
  27. end
  28.  
  29. -- Example: Disable collisions when 'E' key is pressed
  30. game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
  31.     if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.E then
  32.         disableCollisions()
  33.     end
  34. end)
  35.  
  36. -- Example: Enable collisions when 'R' key is pressed
  37. game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
  38.     if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.R then
  39.         enableCollisions()
  40.     end
  41. end)
Advertisement
Add Comment
Please, Sign In to add comment