My rope tool isnt going to the direction of the mouse

So I got the rope tool, But when I click to use it the rope goes to the middle of the part I clicked, I want it to go to where the player clicks but it is going automatically to the middle of the parts.

Could someone help me?

image
This is the local script of the tool


And this is the script of the tool

Here is a image:

image (55)

(Note;I am very bad at scripting so if you could write the right script thats a big help)

1 Like

There’s a built in thing inside the mouse that can get you its position, so use Mouse.(the function) and then create an Attachment on that position, and link it to a Rope object linked with the player.

Thanks for your reply, So as I said I dont know almost nothing about scripting, May I ask if you could write the script here? if yes Thats a big help.

If you put an attachment inside a part, it will automatically be centered.

You can do this by using Mouse.Hit.Position :

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Up:Connect(function() -- Fires when MB1 is UP(finger raised from mb1), not when DOWN(finger pressed).
	local attach = Instance.new("Attachment") -- Create the attachment that will be used on the part.
	attach.Parent = Mouse.Target -- Parent it to the object that the player's mouse is pointing to.
	attach.Position = Mouse.Hit.Position -- Set the attachment's position to the mouse's position.
	
	local attach2 = Instance.new("Attachment")  -- Create the attachment that will be used on the player.
	attach2.Parent = Player.Character:FindFirstChild("HumanoidRootPart") -- Parent it to the player's HumanoidRootPart.
	
	local rope = Instance.new("RopeConstraint") -- Create the rope.
	rope.Parent = workspace -- Parent it to whatever you want, but parent it to something under the Workspace.
	rope.Length = (attach.WorldCFrame.Position - attach2.WorldCFrame.Position).Magnitude -- Set the length of the rope to the distance between the 2 attachments.
	rope.Attachment0 = attach -- It doesn't matter which attachment set to which.
	rope.Attachment1 = attach2
	rope.Visible = true -- Make it visible because it's not by default
end)

Notes:

  • This is from a LocalScript, you need to use RemoteEvents in order to replicate this action from the server. lf you don’t it will just be visible to the player.

  • You probably would want to execute the function when MB1 is up because then player can don’t release the mouse to not place the rope, move it somewhere else then place it there if they’ve changed their mind out of nowhere. You can use MB1 Down if you’re evil >:) (Joking, you can use any Mouse.Event)

  • We used WorldCFrame because we want to get the position of the attachments in the world space coordinates, not in their parents.

1 Like

I used the script but a strange thing happened,when I click the rope appears in the air at a random place and If I click again more ropes appear( even if I dont equip rope tool)

I’ve been trying to fix this simple mistake for about 2 hours, my last post was completely revising and remaking the script with using a part’s location that we creat instead of the attachment’s. After I posted it I realised someting; turns out I accidentally wrote attach.Position = Mouse.Hit.Position instead I should’ve wrote attach.WorldPosition = Mouse.Hit.Position :upside_down_face:, previous one was setting the position of the attachment’s position based on it’s parent and not the world space.

Here is the edited code, works just fine:

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Up:Connect(function() -- Fires when MB1 is UP(finger raised from mb1), not when DOWN(finger pressed).
	local attach = Instance.new("Attachment") -- Create the attachment that will be used on the part.
	attach.Parent = Mouse.Target -- Parent it to the object that the player's mouse is pointing to.
	attach.WorldPosition = Mouse.Hit.Position -- Set the attachment's position to the mouse's position.

	local attach2 = Instance.new("Attachment")  -- Create the attachment that will be used on the player.
	attach2.Parent = Player.Character:FindFirstChild("HumanoidRootPart") -- Parent it to the player's HumanoidRootPart.

	local rope = Instance.new("RopeConstraint") -- Create the rope.
	rope.Parent = workspace -- Parent it to whatever you want, but parent it to something under the Workspace.
	rope.Length = (attach.WorldCFrame.Position - attach2.WorldCFrame.Position).Magnitude -- Set the length of the rope to the distance between the 2 attachments.
	rope.Attachment0 = attach -- It doesn't matter which attachment set to which.
	rope.Attachment1 = attach2
	rope.Visible = true -- Make it visible because it's not by default
end)

Also, apologies for making you wait this long.

1 Like

Now the rope is going to the direction of the mouse.but, It doesnt disappear when I click again and I still can spawn many ropes by clicking many times.

I used your new script as a local script and I am using another script inside it

here is the other script:

You can do this by checking if a rope already exist when the tool is used, if false; just create a new rope, if true; delete the old instances from the rope you created, and make a new one. You can do an if statement to check if yourInstance == nil or ~= nil(not equal to).

By the way, it’s easier for me to check your code if you use a preformatted text instead of an image.

There is already a if rope == nil and if newrope == nil

> local rope
> local att0
> local att1
> local newRope
> 
> script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr,target,hit)
> 	if rope == nil then
> 		if target == nil then return end
> 		if target:IsA('Sky') or target:IsA('Atmosphere') or target == nil then return end
> 		if target.Name ~= 'Baseplate' then
> 			if (script.Parent.Parent.Parent.HumanoidRootPart.Position - target.Position).Magnitude > 30 then return end
> 			rope = Instance.new('RopeConstraint')
> 			att0 = Instance.new('Attachment')
> 			att1 = Instance.new('Attachment')
> 		att0.Parent = target
> 		att1.Parent = script.Parent.Parent.Parent.HumanoidRootPart
> 		rope.Visible = true
> 		rope.Parent = script.Parent.Parent.Parent.HumanoidRootPart
> 		rope.Attachment0 = att0
> 		rope.Attachment1 = att1
> 			rope.Length = (script.Parent.Parent.Parent.HumanoidRootPart.Position - target.Position).Magnitude
> 		else
> 			newRope = game.ServerStorage.ForRope:Clone()
> 			newRope.Parent = workspace
> 			newRope.CFrame = hit
> 			task.wait(0.05)
> 			if (script.Parent.Parent.Parent.HumanoidRootPart.Position - newRope.Position).Magnitude > 30 then newRope:Destroy() newRope = nil return end
> 			rope = Instance.new('RopeConstraint')
> 			att0 = Instance.new('Attachment')
> 			att1 = Instance.new('Attachment')
> 			att0.Parent = newRope
> 			att1.Parent = script.Parent.Parent.Parent.HumanoidRootPart
> 			rope.Visible = true
> 			rope.Parent = script.Parent.Parent.Parent.HumanoidRootPart
> 			rope.Attachment0 = att0
> 			rope.Attachment1 = att1
> 			rope.Length = (script.Parent.Parent.Parent.HumanoidRootPart.Position - newRope.Position).Magnitude
> 			
> 			end
> 	else
> 		rope:Destroy()
> 		att0:Destroy()
> 		att1:Destroy()
> 		rope = nil
> 		att0 = nil
> 		att1 = nil
> 		if newRope ~= nil then
> 			newRope:Destroy()
> 			newRope = nil
> 		end
> 		end
> end)
> 
> script.Parent.Parent.Unequipped:Connect(function()
> 	if rope ~= nil then
> 		rope:Destroy()
> 		att0:Destroy()
> 		att1:Destroy()
> 		rope = nil
> 		att0 = nil
> 		att1 = nil
> 		if newRope ~= nil then
> 			newRope:Destroy()
> 			newRope = nil
> 		end
> 	end
> end)

You can print() statements on each section of your code to see which part works and which part doesn’t. I can’t check it myself because that was a quote that you wrote, you can use CTRL + E to create a preformatted text on DevForum.

Yes,I didnt know how to do it,now I fixed the text format.

by the way I tried a lot of things but I still didnt solve this problem, I dont know why it isnt dissapearing after a second click and I can spawn many ropes, that script was supposed to do those things but it isnt doing.

Pardon me if Im wrong but I dont think you used the hit parameter in your server script a single time, and since hit is the position of the mouse it isnt suprising that the rope doesnt go to the position of the mouse… What I would do is on line 21 replace target.position with hit.Position when calculating the length of the string and after line 15 (creating attachment 0), insert the line

att0.CFrame = hit:ToObjectSpace(target.CFrame) --sets the position of the attachment to hit. since attachments use relative space rather than worldspace, we need to use :ToObjectSpace()

Thank you! now it is almost everything right, the only problem is that when I click to launch the rope my character is teleported in the air (he almost goes inside the part that I clicked).

The image:

what is it setting the length of the rope to? can you check in the properties tab
also can i see your code for calculating the length of the rope?

if you still cant get it maybe try a mouse raycast that moves the rope to the mouse

There is only two scripts in the rope those I already put the image here,also I dont know where you are saying me to check at properties tab.

1 Like

can i see


and if you changed the length calculation code at all can i see that too?

Hm there is no ropeconstraint in my workspace,there is only the scripts inside the rope tool and a remote event

image