Issue with LookVector accuracy

I’m trying to make a knockback script but the knockback just moves the enemy the wrong way.

Script:

tool = script.Parent
handle = tool.Handle
enabled = true
local hits = {}

local function attack()
	if enabled == false 
	then return end
	enabled = false
	
	handle.swing:Play()
	
	handle.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		local char = hit.Parent
		local root = char:FindFirstChild("HumanoidRootPart")
		local otherroot = script.Parent.Parent:FindFirstChild("HumanoidRootPart")
		if hum ~= nil and not hits[char] and root ~= nil and enabled == false then
			
			hits[char] = true
			hum:TakeDamage(50)
			local lookvector = otherroot.CFrame.LookVector
			
			task.wait()
			
			local velo = Instance.new("BodyVelocity")
			velo.Parent = root
			velo.MaxForce = Vector3.new(9999,9999,9999)
			
			velo.Velocity = lookvector * 1000
			
			handle.stab:Play()
			
			if hum.Health <= 0 then
				task.wait(0.25)
				handle.ban:Play()
			end
			
			task.spawn(function()
				task.wait(2)
				hits[char] = false
			end)
			
			task.wait(0.25)
	
			velo:Destroy()
			
		end
	end)
	
	task.wait(2)

	enabled = true
	
end

tool.Activated:Connect(attack)

Additional Image

Screenshot 2024-09-02 164824

It just for some reason moves the dummy to the left.

1 Like

Have you tried changing before the properties of the BodyVelocity then parenting it to the enemy root part?

Also, everytime you attack, you are creating a new .Touched function, which will eventually accumulate so much that everytime the ban hammer hits your game will lag to 1 frame per second. You must have the function separetely from the attack function, it should work fine since you added a enabled variable.

I did the parenting after I changed the properties, nothing changed.

For the .Touched function I took your advice and changed it. Thanks!

Ended up fixing it.

Rather weird solution, but it works.

Thank you for attempting to help, anyways @Jexemie .

tool = script.Parent
handle = tool.Handle
enabled = true
local hits = {}

tool.Activated:Connect(function()
	if enabled == false 
	then return end
	enabled = false
	
	handle.swing:Play()
	
	handle.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		local char = hit.Parent
		local root = char:FindFirstChild("HumanoidRootPart")
		local otherroot = script.Parent.Parent:FindFirstChild("HumanoidRootPart")
		if hum ~= nil and not hits[char] and root ~= nil and enabled == false then
			
			hits[char] = true
			hum:TakeDamage(50)
			local lookvector = otherroot.CFrame.LookVector * Vector3.new(0,0,45)
			
			task.wait()
			
			local velo = Instance.new("BodyVelocity")
			velo.MaxForce = Vector3.new(99999,99999,99999)			
			velo.Velocity = lookvector * 2.5
			
			task.wait()
			
			velo.Parent = root
			
			handle.stab:Play()
			
			if hum.Health <= 0 then
				task.wait(0.25)
				handle.ban:Play()
			end
			
			task.spawn(function()
				task.wait(2)
				hits[char] = false
			end)
			
			task.wait(0.25)
	
			velo:Destroy()
			
		end
	end)
	
	task.wait(2)

	enabled = true
	
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.