So i wanted to make a grappling hook, the ones where they have a rope that lets you climb, it creates a “rope”. I want to make it so that for every raycast hit, it creates a rope, with the appropiate size and position. The problem is, Its not quite working as i expected it to.
External Media
local function visualray(origin,respos)
local dist = (origin - respos).Magnitude
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
part.Size = Vector3.new(0.1,0.1,dist)
part.CFrame = CFrame.new(origin,respos)*CFrame.new(0,0,-(dist/2))
part.Material = Enum.Material.Plastic
debirs:AddItem(part,1)
end
cast.LengthChanged:Connect(function(activecast,lastpointray,raydir,displacement,segmentvelocity,cosmeticbulletobject)
local lenght = cosmeticbulletobject.Size.X/2
local offset = CFrame.new(0,0,-(displacement - lenght))
local directionray = cosmeticbulletobject.Position + Vector3.new(0,-30,0)
cosmeticbulletobject.CFrame = CFrame.lookAt(lastpointray,lastpointray+raydir):ToWorldSpace(offset)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {Character,Character.Grapplinghook.Handle,cosmeticbulletobject}
local raycastresult = workspace:Raycast(cosmeticbulletobject.Position,directionray,params)
visualray(cosmeticbulletobject.Position,directionray)
if raycastresult then
print(raycastresult.Position)
lastYpos = raycastresult.Position.Y
local ropepart = game.ReplicatedStorage.rope:Clone()
local initialposition = ropepart.endofrope.Position
local initialsize = ropepart.Size
local dist = (lastpoint - raycastresult.Position).Magnitude
local dir = (lastpoint - raycastresult.Position).Unit
ropepart.Size = Vector3.new(0.1,0.1,dist)
ropepart.CFrame = CFrame.new(lastpoint,raycastresult.Position) * CFrame.new(0,0, -dist/2)
local newSize = ropepart.Size
local scaleFactorFromOriginal = (newSize)/initialsize
ropepart.endofrope.Position = initialposition*scaleFactorFromOriginal
ropepart.Name = "rope"
ropepart.Parent = game.Workspace.Rope
lastpoint = raycastresult.Position
end
end)
cast.RayHit:Connect(function(cast,raycastresult,segmentvelocity,cosmeticbulletobject)
cosmeticbulletobject:Destroy()
lastYpos = 0
lastpoint = nil
end)
RE.OnServerEvent:Connect(function(player,state,mousepos,handle,lastXposRE,lastYposRE,lastZposRE)
Character = player.Character
local origin = handle.Shoot.FirePoint.Position
lastpoint = player.Character.HumanoidRootPart.Position
local dir = (mousepos - origin).Unit
cast:Fire(handle.Shoot.FirePoint.WorldPosition,dir,100,behavior)
end)
NOTE: Im using fastcast for the grapplehook, but im using raycastresults for my rope. I believe there is a problem with the raycastresult since everytime it prints the raycastresult.Position, it brings me to the rope, also im using an attachment for the rope which moves everytime the rope gets sized
i also didnt copy the variables on the first lines since i dont think they are important atm