Help with cape script

I have taken a function used for a cape in an old Script Builder script, and am trying to use it for my cape system. However, I am struggling to customize it.

Basically I want it so when you walk backwards it doesn’t go through your chest like this:

https://gyazo.com/254eec5d6987b0df312f007b0224b447

I also want to change it so it doesn’t go to a practically 90-degree angle when you’re walking because it looks kinda weird. I’ve tried messing around with it but a lot of things I change don’t really make a difference, and I barely understand what’s happening in the code.

Here it is:

local RunService = game:GetService("RunService")
local PlayersService = game:GetService("Players")
local TweenService	=  game:GetService("TweenService")
 
local Player = PlayersService.LocalPlayer

local WorldUp = Vector3.new(0,1,0)
local Capes = {}


function look2(Vec1,Vec2)
   local Orig = Vec1
   Vec1 = Vec1+Vector3.new(0,1,0)
   Vec2 = Vec2+Vector3.new(0,1,0)
   local Forward = (Vec2-Vec1).unit
   local Up = (WorldUp-WorldUp:Dot(Forward)*Forward).unit
   local Right = Up:Cross(Forward).unit
   Forward = -Forward
   Right = -Right
   return CFrame.new(Orig.X,Orig.Y,Orig.Z,Right.X,Up.X,Forward.X,Right.Y,Up.Y,Forward.Y,Right.Z,Up.Z,Forward.Z)
end
 
function look(CFr,Vec2)
   local A = Vector3.new(0,0,0)
   local B = CFr:inverse()*Vec2
   local CF = look2(A,Vector3.new(A.X,B.Y,B.Z))
   if B.Z > 0 then
       CF = CFr*(CF*CFrame.Angles(0,0,math.pi))
   elseif B.Z == 0 then
       if B.Y > 0 then
           CF = CFr*CFrame.Angles(math.pi/2,0,0)
       elseif B.Y < 0 then
           CF = CFr*CFrame.Angles(-math.pi/2,0,0)
       else
           CF = CFr
       end
   end
   local _,_,_,_,X,_,_,Y,_,_,Z,_ = CF:components()
   local Up = Vector3.new(X,Y,Z)
   local Forward = (Vec2-CFr.p).unit
   local Right = Up:Cross(Forward)
   Forward = -	Forward
   Right = -Right
   return CFrame.new(CFr.X,CFr.Y,CFr.Z,Right.X,Up.X,Forward.X,Right.Y,Up.Y,Forward.Y,Right.Z,Up.Z,Forward.Z)
end
 
function simulate(j,d,m,r,t)
	local joint = j
	for i,v in ipairs(t) do
		if v[1]:FindFirstChild("Weld") then
			local stiff = m.CFrame.lookVector*0.01
			if i > 1 then joint = t[i-1][1].CFrame*CFrame.new(0,0,d*.5) end
			local dir = (v[2].p-(joint.p+Vector3.new(0,0.2,0)+stiff)).Unit
			local dis = (v[2].p-(joint.p+Vector3.new(0,0.2,0)+stiff)).Magnitude
			local pos = joint.p+(dir*(d*0.5))
			--if v[1].CFrame.y<=workspace.Base.CFrame.y then pos = joint.p+(dir*(d*.5)) end
			local inv = v[1].Weld.Part0.CFrame
			local rel1 = inv:Inverse()*pos
			local rel2 = inv:Inverse()*(pos-(dir*dis))
			local cf = look(CFrame.new(rel1),rel2)--CFrame.new(pos,pos-(dir*dis))*CFrame.fromEulerAnglesXYZ(r.x,r.y,r.z)
			
			--v[1].Weld.C0 = cf
			--v[2] = inv*cf
			
			local CFrame1 = v[2]
			local CFrame2=inv * cf
			
			local CFrame3 = v[1].Weld.C0
			local CFrame4= cf	
			
			for i=0,1,0.2 do
				v[2]=CFrame1:lerp(CFrame2,i)
				v[1].Weld.C0=CFrame3:lerp(CFrame4,i)
				RunService.Heartbeat:Wait()
			end
			
			--v[1].CFrame = cf
		 end
	end
end



RunService.Stepped:Connect(function()
	for _,v in pairs(Capes) do
		simulate(v["Torso"].CFrame*CFrame.new(0,0.9,.5),.6,v["Torso"],Vector3.new(),v["Cape"])
	end
end)

function Handler(Player)
	Player.CharacterAdded:Connect(function(Char)
		wait(0.5)
		local HRP = Char:WaitForChild("HumanoidRootPart")
		local Torso = Char:WaitForChild("Torso")
		local Tab2 = {}
		local Tab = {["Torso"] = Torso; ["Cape"] = Tab2;["Tween"] = nil;["Tween"] = nil}
		for i=1,1 do
			local p = Instance.new("Part",Tab["Torso"])
			p.Anchored = false
			p.BrickColor = 		BrickColor.new("Really red")
			p.CanCollide = 		false
			p.FormFactor=		Enum.FormFactor.Custom
			p.Material = 		Enum.Material.SmoothPlastic
			p.TopSurface = 		Enum.SurfaceType.SmoothNoOutlines
			p.BottomSurface = 	Enum.SurfaceType.SmoothNoOutlines
			p.RightSurface = 	Enum.SurfaceType.SmoothNoOutlines
			p.LeftSurface = 	Enum.SurfaceType.SmoothNoOutlines
			p.FrontSurface = 	Enum.SurfaceType.SmoothNoOutlines
			p.BackSurface = 	Enum.SurfaceType.SmoothNoOutlines
			 
			p.Size=Vector3.new(2,.3,1)
			p:BreakJoints() -- sometimes the parts are stuck to something so you have to breakjoints them
			local mesh = Instance.new("BlockMesh",p)
			mesh.Scale = Vector3.new(1,1,4)
			mesh.Offset = Vector3.new(0,0,1.5)
			local w = Instance.new("Motor6D",p)
			w.Part0 = Tab["Cape"][i-1] and Tab["Cape"][i-1][1] or Tab["Torso"]
			w.Part1 = p
			w.Name = "Weld"
			--table.insert(aa1,p)
			Tab["Cape"][i] = {p,p.CFrame,nil}
		end
		table.insert(Capes,Tab)
	end)
end

Handler(Player)
1 Like

Sorry, but don’t feel like figuring out all those variable letters.

What you want to do is something like
clamp the cape to within 90? degrees of the negative character lookVector to keep it behind them.

maybe the Z component? if - make it 0?

Following on from what Pharyx_Styx said, how do you expect us to understand what is happening if you don’t understand yourself? :thinking:

I’d recommend starting from scratch rather than ripping code you don’t understand in the slightest- there are plenty of great capes out there that I’ve seen and you could easily make your own even by simply using something like attachments and beams!

2 Likes

I have started doing that, however I am unsure how to check if a players torso is moving forwards (not using humanoid.movedirection)

Where it is placed in file system

I mean you could always check the torso’s velocity

Following on from what I said about attachments, you could even use a spring connected to the humanoid root part with a prismatic constraint :thinking:

I think I can just have somewhere cape model and I copy it into torso and depending on speed make its angle

I’ve finished the cape, using HingeConstraint and all, but to activate it I don’t know how to make it so its only when the torso is moving forwards, cause if u shiftlock backwards it does it as well.

And I dont think prismatic constraints are the best for this.

https://gyazo.com/f86b7475a20a6350c55d35eb5eeac19a

1 Like

you should be able to set “limits enabled”- that way it’ll stop at a certain angle and won’t go through your body