Optimizing Collision Script

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 am not sure what exactly you need this to do. Characters should spawn in with most parts CanCollide-able.

Can you answer what you expect this script to do? and answer what it is currently doing wrong?

1 Like

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

And what is it currently doing wrong?

When i jump, it disables collisions, also it checks every 10 secs. which isnt this convenient

Ok, do any of your other scripts mess with the player on jump? I see you have some sort of size changing UI, did you write any code for that?

Seems like it could be the AntiFloatyGiantScript, the Tree Server, or the StarterGUI.Poses.Animations.AnimScript

1 Like

Why do you have to checkAndEnableCollisions every 10 seconds?

Cause sometimes they get disabled for some reason, so that sets them back to enabled

part:GetPropertyChangedSignal("CanCollide"):Connect(function()
    if not part.CanCollide then part.CanCollide = true end
end)
1 Like

I’m gonna try that as soon as I get home, thanks for helping out, I’ll let you know

nope, it still disables collisions when jumping

-- 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)