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

How would i go about can - colliding a character's body parts?

So i am making a ragdoll script, and it looks pretty weird.

you could test this out with a script that i will give you below (make a local script inside of starter character scripts.)

full script:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
humanoidRootPart.CanCollide = false
local humanoid = char:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local uis = game:GetService("UserInputService")

local hasRagdolled = false

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		if hasRagdolled == false then
			hasRagdolled = true
			for _, v in pairs(char:GetDescendants()) do
				if v:IsA("Motor6D") then
					if v.Name ~= "" then
						camera.CameraSubject = humanoidRootPart
						humanoid.PlatformStand = true
						humanoid.WalkSpeed = 0
						humanoid.JumpPower = 0
						v.Enabled = false
						local a0 = Instance.new("Attachment")
						a0.CFrame = v.C0
						a0.Parent = v.Part0
						local a1 = Instance.new("Attachment")
						a1.CFrame = v.C1
						a1.Parent = v.Part1
						local ballSocketConstrait = Instance.new("BallSocketConstraint")
						ballSocketConstrait.Parent = v.Part0
						ballSocketConstrait.Attachment0 = a0
						ballSocketConstrait.Attachment1 = a1
					end
				end
			end
			
			for _, v in pairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					if v.Name ~= "HumanoidRootPart" then
						v.CanCollide = true
					end
				end
			end
			
		elseif hasRagdolled == true then
			hasRagdolled = false
			humanoid.JumpPower = 50
			humanoid.WalkSpeed = 16
			humanoid.PlatformStand = false
			for _, v in pairs(char:GetDescendants()) do
				if v:IsA("BallSocketConstraint") then
					camera.CameraSubject = humanoid
					v:Destroy()
				end
				if v:IsA("Motor6D") then
					v.Enabled = true
				end
				
				for _, v in pairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					v.CanCollide = false
				end
			end
				
			end
		end
	end
end)

what i need help with:

local char = game.Players.LocalPlayer.Character
			
for _, v in pairs(char:GetChildren()) do
		if v:IsA("BasePart") then
		if v.Name ~= "HumanoidRootPart" then
			v.CanCollide = true
		end
	end
end

hopefully someone can help me with this.

1 answer

Do you have experience with collision groups? you could put them in a collision group and then put them in a different one when you want them to not collide

local phyicsService = game:GetService('PhysicsService')

local plr, other = 'Player', 'Other' -- groups

local setPlr = phyicsService:CreateCollisionGroup(plr)
local setNpc = phyicsService:CreateCollisionGroup(other)

phyicsService:CollisionGroupSetCollidable(plr, other, false)

function addToCollisionGroup(mod, group)
	for k, v in pairs(mod:GetChildren()) do
		if v:IsA('BasePart') then
			phyicsService:SetPartCollisionGroup(v, group)
		end
	end
end

addToCollisionGroup(workspace, other)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		addToCollisionGroup(char, plr) -- (model of player, player collision group)
	end)
end)

and when you need to change it you can do this:

phyicsService:CollisionGroupSetCollidable(plr, other, true)

hope this helped!

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