I am creating a game in which my character connects to a part using a rope, but I need him to connect to the nearest part. All parts have the same name; if you give everyone a different name, it will cause a lot of difficulties. I know what magnitude is, but I do not know how to use it correctly in a given situation, or whether it should be used at all
Sorry for the mistakes, English is not my native language
link if does work ibb.co/PcKfjWP http://ibb.co/vqXvTHv
script
local debris = game:GetService("Debris")
game.ReplicatedStorage.SwinningEvent.OnServerEvent:Connect(function(player)
local character = game.Workspace:WaitForChild(player.Name)
local sound = game.ReplicatedStorage.swing:Clone()
--Settings
local swingdistance = 100
--FindPartAndConnect
if game.Workspace:GetChildren("SwingPart") then
if (character.HumanoidRootPart.Position - game.Workspace.SwingPart.Position).magnitude then
local BV = Instance.new("BodyVelocity", character.HumanoidRootPart)
local RightSwing = character.Humanoid:LoadAnimation(script.SwingRight)
RightSwing:Play()
BV.Velocity = character.HumanoidRootPart.CFrame.lookVector*80
local Rope = Instance.new("RopeConstraint", game.Workspace.SwingPart)
Rope.Length = (character.HumanoidRootPart.Position - game.Workspace.SwingPart.Position).magnitude
Rope.Thickness = 0.05
Rope.Color = BrickColor.new("Institutional white")
Rope.Visible = true
local attachment0 = Instance.new("Attachment",game.Workspace.SwingPart )
attachment0.Name = "Attachment0"
local attachment1 = Instance.new("Attachment",character.RightHand )
attachment1.Name = "Attachment1"
Rope.Attachment0 = attachment0
Rope.Attachment1 = attachment1
sound.Parent = workspace
sound:Play()
debris:AddItem(sound,1)
end
end
end)
--Ended Swing
game.ReplicatedStorage.SwingEnd.OnServerEvent:Connect(function(player)
local style1 = script.Style1
local style2 = script.Style2
local character = game.Workspace:WaitForChild(player.Name)
local RightSwing = character.Humanoid:LoadAnimation(script.SwingRight)
RightSwing:Stop()
local BV = character.HumanoidRootPart.BodyVelocity
BV.Velocity = character.HumanoidRootPart.CFrame.lookVector*40 + character.HumanoidRootPart.CFrame.upVector*80
debris:AddItem(game.Workspace.SwingPart.RopeConstraint,0 )
debris:AddItem(character.HumanoidRootPart.BodyVelocity,1)
local AnimA = character.Humanoid:LoadAnimation(style1)
local AnimB = character.Humanoid:LoadAnimation(style2)
local Number = math.random(1,2)
if Number == 1 then
AnimA:Play()
else if Number == 2 then
AnimB:Play()
end
end
end)

3 comments
Can we see your script?
of course
You need a script, this is not a request site.