Hi, i’ve made this script to give a player collisions, but it doesn’t work well and it’s inefficient, how can i fix this?
code:
-- Define a function to check and enable collisions for each player's parts
local function checkAndEnableCollisions(player)
local character = player.Character
if not character then return end
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") and part ~= character.HumanoidRootPart and part.Name ~= "Handle" and part.Name ~= "LeftFoot" and part.Name ~= "RightFoot" then
if not part.CanCollide then
part.CanCollide = true
end
end
end
end
-- Loop through each player in the game and connect to the character added event
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Enable collisions initially when the character is added
checkAndEnableCollisions(player)
end)
end)
-- Continuously check and enable collisions for each player's parts
while true do
for _, player in ipairs(game.Players:GetPlayers()) do
checkAndEnableCollisions(player)
end
wait(10) -- Adjust the delay between checks if needed
end
I’m trying to make a script that sets EVERY player part collidable (including arms etc), excluding the left and right feet, accessories and HumanoidRootPart
-- Define a function to check and enable collisions for each player's parts
local function checkAndEnableCollisions(player)
local character = player.Character
if not character then return end
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") and part ~= character.HumanoidRootPart and part.Name ~= "Handle" and part.Name ~= "LeftFoot" and part.Name ~= "RightFoot" then
if not part.CanCollide then
part.CanCollide = true
end
end
end
end
-- Connect to the CharacterAdded event for each player
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Enable collisions initially when the character is added
checkAndEnableCollisions(player)
end)
-- Connect to the CanCollide property change event for each part
for _, part in pairs(player.Character:GetDescendants()) do
if part:IsA("BasePart") and part.Name ~= "Handle" and part.Name ~= "LeftFoot" and part.Name ~= "RightFoot" then
part:GetPropertyChangedSignal("CanCollide"):Connect(function()
if not part.CanCollide then
part.CanCollide = true
end
end)
end
end
end)