Script works but not as intended

im making a combat system, and i have this knockback system which works but not as i intended it to work.


(Im not sure why the GUI is like that, its usually working fine.)

But the issue is that the dummy turns around and doesnt face the player that hits it. I want the dummy to face the player that hits it but im not sure how. Heres the code

Code (Same for left punch and head slam)
local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")
local REvent = RS.Events.ClickCombat:WaitForChild("Right")	
local db = false

REvent.OnServerEvent:Connect(function (plr)
	local hitbox = Instance.new("Part")
	local weld = Instance.new("Weld")
	print(plr.Name)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local RootPart = char:WaitForChild("HumanoidRootPart")
	local rArm = char:WaitForChild("Right Arm")
	---------------------------------
	local MoveTween = TS:Create(RootPart, TweenInfo.new(.5), {CFrame = RootPart.CFrame * CFrame.new(0,0,-2)})
	print(RootPart.CFrame)
	MoveTween:Play()
	---------------------------------
	
	rArm.Touched:Connect(function(hit)
		if hit:IsA("BasePart") then
			local hum = hit.Parent:WaitForChild("Humanoid")
			local humPart = hit.Parent:FindFirstChild("HumanoidRootPart")
			local animation = Instance.new("Animation")

			animation.AnimationId = "rbxassetid://8248301937"
			local track = hum:LoadAnimation(animation)
			local lookDirect = humPart.CFrame.LookVector
			---------------------------
			local power = 4
			local lookDirect = RootPart.CFrame.lookVector
			local origin = RootPart.CFrame
			local knockback = lookDirect*power
			local tPos = origin + knockback

			local tween = TS:Create(humPart, TweenInfo.new(1), {CFrame = tPos})

			if hum and hit:FindFirstAncestorWhichIsA("Model") then
				hitbox.Position = humPart.Position
				hitbox.Size = humPart.Size
				hitbox.Parent = hit.Parent
				hitbox.Anchored = false
				hitbox.Transparency = 1

				weld.Parent = hitbox
				weld.Part0 = humPart
				weld.Part1 = hitbox
				hitbox.Touched:Connect(function(per)
					if db == false then
						db = true
						hum:TakeDamage(5)
						if hum.Health <= hum.MaxHealth then
							db = true
							track:Play()
							tween:Play()
							hum.WalkSpeed = 0
							task.wait(.3)
							hum.WalkSpeed = 16
							db = false
						end
						task.wait(1.5)
						hitbox:Destroy()
						db = false
					end
				end)
			end
		end
	end)
end)

Help is appreciated, thank you for your time :grinning:

Hi!

Try this;

local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")
local REvent = RS.Events.ClickCombat:WaitForChild("Right")	
local db = false

REvent.OnServerEvent:Connect(function (plr)
	local hitbox = Instance.new("Part")
	local weld = Instance.new("Weld")
	print(plr.Name)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local RootPart = char:WaitForChild("HumanoidRootPart")
	local rArm = char:WaitForChild("Right Arm")
	---------------------------------
	local MoveTween = TS:Create(RootPart, TweenInfo.new(.5), {CFrame = RootPart.CFrame * CFrame.new(0,0,-2)})
	print(RootPart.CFrame)
	MoveTween:Play()
	---------------------------------

	rArm.Touched:Connect(function(hit)
		if hit:IsA("BasePart") then
			local hum = hit.Parent:WaitForChild("Humanoid")
			local humPart = hit.Parent:FindFirstChild("HumanoidRootPart")
			local animation = Instance.new("Animation")

			animation.AnimationId = "rbxassetid://8248301937"
			local track = hum:LoadAnimation(animation)
			local lookDirect = humPart.CFrame.LookVector
			---------------------------
			local power = 4
			local lookDirect = RootPart.CFrame.lookVector
			local origin = RootPart.CFrame
			local knockback = lookDirect*power
			local tPos = origin + knockback
			
			local lookAt = function(P1, P2)
				return CFrame.new(P1.Position, Vector3.new(P2.Position.X, P1.Position.Y, P2.Position.Z))
			end

			local tween = TS:Create(humPart, TweenInfo.new(1), {CFrame = lookAt(tPos, RootPart)})

			if hum and hit:FindFirstAncestorWhichIsA("Model") then
				hitbox.Position = humPart.Position
				hitbox.Size = humPart.Size
				hitbox.Parent = hit.Parent
				hitbox.Anchored = false
				hitbox.Transparency = 1

				weld.Parent = hitbox
				weld.Part0 = humPart
				weld.Part1 = hitbox
				hitbox.Touched:Connect(function(per)
					if db == false then
						db = true
						hum:TakeDamage(5)
						if hum.Health <= hum.MaxHealth then
							db = true
							track:Play()
							tween:Play()
							hum.WalkSpeed = 0
							task.wait(.3)
							hum.WalkSpeed = 16
							db = false
						end
						task.wait(1.5)
						hitbox:Destroy()
						db = false
					end
				end)
			end
		end
	end)
end)

It pretty much was tweening and looking towards where the other player was looking instead of looking towards the actual player itself.

1 Like

Thank you so much! this worked! i just rewrote the part that you edited in the script, and it worked! :grinning:

I understand what you changed kind of, but a further explanation would be nice so i dont get “spoonfed” scripts :sweat_smile:

You can look more in-depth about CFrame at the ROBLOX API reference, they have really cool info to share about almost anything.

I will say, my version of ‘LookAt’ is deprecated as they have a new function for it, and you can look at how that works there.

Pretty much the two components of CFrame can be used as pos and lookAt, pretty much making a CFrame that is located at pos but is looking towards lookAt.

The reason why it’s a little bit more lengthy is because of this

CFrame.new(P1.Position, P2.Position)

Pretty much, you don’t want the player to be looking exactly at the AI, because it even takes the Y axis into factor.

To fix this, all I did was make lookAt the AI’s position, but the Y is just where the player is to correctly demonstrate looking towards another object.

CFrame.new(P1.Position, Vector3.new(P2.Position.X, P1.Position.Y, P2.Position.Z))