How do i fix this?

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.

How do i fix this?

Take out .Position from Char:WaitForChild("Humanoid"):MoveTo(__Target:GetPrimaryPartCFrame().Position)

Tell me if it works.

MoveTo does not support CFrames.

Oh yeah I forgot. Lol

Try this module
SimplePath - Pathfinding Module - Resources / Community Resources - DevForum | Roblox

to find the players location.

They’re doing Cframe.Position though

The player slows down really really hard when i use the

function. How would i fix that?

Take out the spawn(function() because that sometimes creates lag.

Try this:


Char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(Char:WaitForChild("HumanoidRootPart").Position, __Target:WaitForChild("HumanoidRootPart").Position)

Are there any errors in the Output from that script when you play your game?

If i remove the spawn function, the player will not move at all.There is no way that it will error.

Can I see the whole script so I can debug?

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

Why is there a pcall in this function here?

Incase the opponent is a NPC or something errors. It doesn’t cause the bug.

Yeah, I’m stumped. Sorry but I don’t know how to fix your problem :persevere:

1 Like