The grappling script isn’t functioning as well as I’d expect

Hello, I’m experiencing difficulties with my Spider-Man grappling script, and I can’t get the web to shoot from the player’s hands. The problem is that it just shoots a web out of the torso area, and altering it leads the player to freeze. The code is just as follows: (startercharacterscript)


--//vars//--
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local GrappleRope -- Rope
local Force -- Unnatural force
local ForceAmount = 5510
local pointOne -- Attachment 1
local pointTwo -- Attachment 2
local maxDistance = 500 -- Distance in studs
local grapplePoint = player.Character:WaitForChild("HumanoidRootPart") -- This must be humanoidrootpart or else player will freeze (R15 required)
local mouseDown = false
local badParts = {"Teleporter","NotificationTrigger"}
local canGrapple = false
--//code//--



mouse.Button1Down:connect( function()
    mouseDown = true
end)

mouse.Button1Up:connect( function()
    mouseDown = false
end)

while wait() do
    if mouseDown and player.Character.Humanoid.BodyHeightScale.Value > 0.6 then
				if mouse.Target ~= nil then
					for i=1, #badParts do
						if badParts[i] == mouse.Target.Name then
							canGrapple = false
						else
							canGrapple = true
						end
					end
				end
		  if canGrapple and mouse.Target and GrappleRope == nil and mouse.Target:IsA("Part") and (mouse.Hit.p - grapplePoint.Position).Magnitude < maxDistance  and not mouse.Target.Parent:FindFirstChild("Humanoid") and not mouse.Target:IsA("Accessory") then -- Performs a bunch of checks to make sure rope can be created.
				
				-- Creates Rope and sets properties
				GrappleRope = Instance.new("RopeConstraint", game.workspace)
				GrappleRope.Visible = true
--				GrappleRope.Color = BrickColor.new(Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255)))
				GrappleRope.Color = BrickColor.new("Institutional white")
				GrappleRope.Thickness = 0.07
				
				-- Creates rope attachments, first one is the player, second is mouse aim.
				pointOne = Instance.new("Attachment",grapplePoint)
				pointTwo = Instance.new("Attachment",mouse.Target)
				pointOne.WorldPosition = Vector3.new(grapplePoint.Position.X,grapplePoint.Position.Y,grapplePoint.Position.Z)
				pointTwo.WorldPosition = mouse.Hit.p
				local position = pointTwo
				
				-- Applies ropes attachments
				GrappleRope.Attachment0 = pointOne
				GrappleRope.Attachment1 = pointTwo
				
				-- Sets length of rope
				GrappleRope.Length = (pointOne.WorldPosition - pointTwo.WorldPosition).Magnitude + -2
		
				
				-- Applies unnatural force
				if not grapplePoint:FindFirstChild("BodyForce") then
					Force = Instance.new('BodyForce',grapplePoint)
				Force.Force = camera.CFrame.lookVector * Vector3.new(ForceAmount,5,ForceAmount)
				
				local sound = Instance.new("Sound")
				sound.SoundId = "rbxassetid://4678684009"
				sound.Parent = player.Character.Head
				sound:Play()
				
				local animation = player.Character.Humanoid:LoadAnimation(script.SwingAnimation)
				animation:Play()
				
				end
			end
			if Force ~= nil then
				if Force.Name ~= nil then
					Force.Force = camera.CFrame.lookVector * Vector3.new(ForceAmount,5,ForceAmount)
				end
			end
	else
			if game.Workspace:FindFirstChild("RopeConstraint") then -- Checks if rope already exists, if so, deletes.
				
				game.Workspace.RopeConstraint:Destroy() -- Destsroys rope
				
				if player.Character.HumanoidRootPart:FindFirstChild("BodyForce") then -- Finds the rest of the nonsense, and deletes.
					GrappleRope:Destroy()
					GrappleRope = nil
					pointOne:Destroy()
					pointTwo:Destroy()
					wait(0.25)
					Force:Destroy()
--					wait(0.75)
				end
		    end
	end
end

It looks like your code was cut off - are you able to edit and post the full script, or at least the rest of that while loop?

Also, how are you setting the RopeConstraint’s Attachment0 and Attachment1? Perhaps you could try changing one of those constraints to be attached to player.Character.RightHand (Assuming you’re using R15).

1 Like

Oh yeah i just realised the code was cut off i edited it to the full script have a look

With two minor edits, you can change the script to use the players Right Hand (Or really any other body part!)

-- Line 11
-- Before:
local grapplePoint = player.Character:WaitForChild("HumanoidRootPart")

-- After:
local grapplePoint = player.Character:WaitForChild("RightHand")

-- Line 87
-- Before:
if player.Character.HumanoidRootPart:FindFirstChild("BodyForce") then

-- After
if grapplePoint:FindFirstChild("BodyForce") then

This will create the RopeConstraint at the players hand, and it also improves the code so that you can freely change the body part in the future without issue (Such as at the left hand).

Hope this helps!

1 Like

I appreciate you responding. It does shoot out the players’ hand, but it just freezes the player when you try to use it.

Whoops! Don’t know how I didn’t catch that. With some quick changes, this now seems to work:

-- Line 11
-- You were correct in the initial script: I did not realize that the player literally cannot move unless the constraint is attached to the HRP. 
local grapplePoint = player.Character:WaitForChild("HumanoidRootPart")

-- Line 50
-- Instead of positioning the attachment to the grapplePoint, we just tell it to be positioned at the players hand instead
pointOne.WorldPosition = player.Character.RightHand.Position

One thing I noticed with this change however is that the rope itself does not move if the players hand does - I assume however that your SwingAnimation has the players hand(s) in a static position, which means this should not be an issue.

Also, keep in mind, since this script is in StarterCharacterScripts, and does not use RemoteEvents, ropes will not be visible on the server, or to other clients. If you want help changing this system to use remotes, let me know!

Also I apologize for taking a long while to respond - I work both night and day shifts so I was probably asleep when you responded!

Well, I had some downtime at my work today, so I ended up doing this anyways! I used your script as a baseline, but updated it to use more modern standards and of course, RemoteEvents, to make it multi-player compatible. I commented both scripts if you want to take a look and see whats going on, and if you want any further explanation on any section, let me know!

Here is a screenshot of the workspace layout:
image

And here is a place download so you can try it yourself!
GrappleRope.rbxl (34.7 KB)

Please keep in mind I do not have your SwingAnimation, so you will need to make that a child of ServerSwingController. I assume it will work the way I have it, but if not, let me know and I can update it to be on the localscript instead. Otherwise, it seems to all work as intended.

I can’t wait to see what you make from this!

I’m sorry for bothering you. You shouldn’t have exhausted yourself making this. I appreciate your efforts and all the work you put into this. I sincerly thank you.

1 Like

Absolutely no worries, I’m just glad to help! If you have any questions about anything in it, let me know!

1 Like

Hey, I was going through my old posts and found this old one. I just wanted to say thank you for providing me information and actually helping me and was wondering what do you think my new web swinging mechanics

1 Like