Hey, i am making a sword fighting AI script, and there’s a bug.
This is the part where i know it bugs:
spawn(function()
Char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(Char:WaitForChild("HumanoidRootPart").Position, __Target:WaitForChild("HumanoidRootPart").Position)
end)
... A part of the script later ...
Char:WaitForChild("Humanoid"):MoveTo(__Target:GetPrimaryPartCFrame().Position)
Basically when it flicks me, it suddenly breaks the MoveTo, and moves the player really slow.
--> Q to select player.
--> R to start.
local __Target
local __Run = false
local __Tool = (game:GetService("Players").LocalPlayer.Backpack:FindFirstChildOfClass("Tool") and game:GetService("Players").LocalPlayer.Backpack:FindFirstChildOfClass("Tool"):FindFirstChild("SwordScript").Parent) or (game:GetService("Players").LocalPlayer.Character:FindFirstChildOfClass("Tool") and game:GetService("Players").LocalPlayer.Character:FindFirstChildOfClass("Tool"):FindFirstChild("SwordScript").Parent)
local Player = game:GetService("Players").LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
--// AI \\--
game:GetService("RunService").Stepped:Connect(function(f)
do
if __Run == true and __Target ~= nil and Char:IsDescendantOf(workspace) and Char:WaitForChild("Humanoid", 1337).Health ~= 0 and __Target:WaitForChild("Humanoid").Health ~= 0 then
local dist = (Char:GetPrimaryPartCFrame().Position - __Target:GetPrimaryPartCFrame().Position).Magnitude
local swing = dist/math.cosh(dist)
local sway = (math.sin(tick()^1.175)*swing)
Char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(CFrame.new(Char:WaitForChild("HumanoidRootPart").Position, __Target:WaitForChild("HumanoidRootPart").Position).Position)
if (Char:GetPrimaryPartCFrame().Position - __Target:GetPrimaryPartCFrame().Position).Magnitude <= 13 then
__Tool:Activate()
end
print("GOT HERE")
Char:WaitForChild("Humanoid"):MoveTo(__Target:GetPrimaryPartCFrame().Position)
if dist <= 6 then -- Anti-CMA (Back-up whenever too close.)
Char:WaitForChild("Humanoid"):MoveTo((__Target:GetPrimaryPartCFrame().Position * -__Target:GetPrimaryPartCFrame().LookVector+__Target:GetPrimaryPartCFrame().RightVector*5))
print("CLOSING")
end
else
__Run = false
end
end
end)
--// Connections \\--
game:GetService("Players").LocalPlayer:GetMouse().KeyDown:Connect(function(key)
if key == "q" then
pcall(function()
if game:GetService("Players").LocalPlayer:GetMouse().Target.Parent:FindFirstChildOfClass("Humanoid") then
__Target = game:GetService("Players").LocalPlayer:GetMouse().Target.Parent
end
if game:GetService("Players").LocalPlayer:GetMouse().Target.Parent.Parent:FindFirstChildOfClass("Humanoid") then
__Target = game:GetService("Players").LocalPlayer:GetMouse().Target.Parent.Parent
end
if game:GetService("Players").LocalPlayer:GetMouse().Target.Parent.Parent.Parent:FindFirstChildOfClass("Humanoid") then
__Target = game:GetService("Players").LocalPlayer:GetMouse().Target.Parent.Parent.Parent
end
end)
end
if key == "r" then
__Run = not __Run
end
end)