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.
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.
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.
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 , 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)
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
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).
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.
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).