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

Parts still collideable, after a script tells it to not be collideable not working, why?

Hi, so basically I wanted to make a humanoid, and if you kill it, it obviously falls apart, but you can't collide with them. And after some time, those humanoids despawn. But if I kill them it gives me an error, and I can still collide with them, why?

local a = script.Parent.Head
local b = script.Parent.HumanoidRootPart
local c = script.Parent.LeftArm
local d = script.Parent.LeftLeg
local e = script.Parent.RightArm
local f = script.Parent.RightLeg
local g = script.Parent.Torso

while wait(0.05) do
	if script.Parent.Humanoid.Health <= 0 then
		a.CanCollide = false
		b.CanCollide = false
		c.CanCollide = false
		d.CanCollide = false
		e.CanCollide = false
		f.CanCollide = false
		g.CanCollide = false
		wait(35)
		script.Parent:Destroy()
	end
end

1 answer

Sadly, you cannot make players non-collidable because they have a Humanoid. Another reason is that you're using a local script and not a normal one.

So, how do we fix it? We can use PhysicsService and we can make Collision Groups.

Also you can just do script.Parent:GetChildren() or script.Parent:GetDescendants() instead of listing each limb one by one.

local PhysicsService = game:GetService("PhysicsService")

local character = script.Parent
local CharacterGroup = PhysicsService:CreateCollisionGroup("Character") -- creates a collision group for your/someone's character
local EverythingExitsed = PhysicsService:CreateCollisionGroup("Instances") -- creates a collision group of every parts that existed in the workspace except the character
PhysicsService:CollisionGroupSetCollidable("Character", "Instances", false) -- makes both collision groups to not collide with each other

local Humanoid = character:FindFIrstChildOfClass("Humanoid")

function NoCollide(char)
	for i, v in pairs(char:GetDescendants()) do
		if v:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(v, "Character")
		end
	end
end

Humanoid.Died:Connect(function() -- alternative to `if Humanoid.Health <= 0 then`
	local everything = workspace:GetDescendants()
	
	NoCollide(character)
	for i, v in ipairs(everything) do
		if v:IsDescendantOf(character) then
			table.remove(everything, i)
		elseif v == character then
			table.remove(everything, i)
		elseif not v:IsA("BasePart") then
			table.remove(everything, i)
		end
		
		PhysicsService:SetPartCollisionGroup(v, "Instances")
	end
end)

2 comments

0

Quite a nice idea, but it still gives me an error, and doesn't work

0

what's the error?

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