Knockback script not working

This script is meant to make the player fly back when they are hit by this part, I tested it on a test game then put it into my main game but for some reason when it’s in the main game it doesn’t work, and I can’t figure out why.

script.Parent.Touched:Connect(function(h)
	local speed = 400
	local force = 80000
	
	local kb = Instance.new("BodyVelocity")
	kb.Parent = h.Parent:FindFirstChild("HumanoidRootPart")
	kb.MaxForce = Vector3.new(force,force,force)
	kb.Velocity = script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector * speed
	wait(0.1)
	kb.MaxForce = Vector3.new(0,0,0)
end)
2 Likes

Can you try replacing wait(0.1) and the line below it with:

game.Debris:AddItem(kb, 0.1) ?

If that doesn’t work, I think it has to do with your velocity equation!

it worked in the test server so I don’t think it’s the equation

I tried that still doesn’t work, I’m so confused

Is this a Local Script or a Server Script?

it’s a server script (30 chars)

How are you giving FireServer?

I changed that part, now it’s when the player touches the part

script.Parent.Touched:Connect(function(h)
	local speed = 400
	local force = 80000
	
	local kb = Instance.new("BodyVelocity")
	kb.Parent = h.Parent:FindFirstChild("HumanoidRootPart")
	kb.MaxForce = Vector3.new(force,force,force)
	kb.Velocity = h.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector * speed
	wait(0.1)
	kb.MaxForce = Vector3.new(0,0,0)
end)

test

1 Like

Hey I was looking at ur script and I think the problem is that u are getting humanod root part as a parent and u arent totally sure if the parent of hit is a player. What if u hit baseplate? U are getting nil and then an error which will cause the script to break

1 Like

U need to do if statement checking for HumanoidRootPart

1 Like

oh it might be that I’ll try to do that

1 Like
local Part = script.Parent

Part.Touched:Connect(function(Touched)
	if Touched.Parent:FindFirstChild("HumanoidRootPart") then
		local BodyVelocity = Instance.new("BodyVelocity", Touched.Parent.HumanoidRootPart)
		BodyVelocity.MaxForce = Vector3.new(80000, 80000, 80000)
		BodyVelocity.Velocity = Touched.Parent.HumanoidRootPart.CFrame.LookVector * 400
		
		wait(0.1)
		
		BodyVelocity = Vector3.new(0, 0, 0)
	end
end)

something like it

1 Like

isn’t that the same as my script

1 Like

Well I just recreated a new one using the values ​​you put above, checking if the HumanoidRootPart is present

oh yea thx, it’s resolved now tho