Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

i need help with making knockback for a sword?

i have tried multiple things including this piece of code that does not work for some reason

local direction = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector * 10000

script.Parent.Blade.Touched:Connect(function(p)
	
	if script.Parent.CanDamage.Value == true then
	
		script.Parent.CanDamage.Value = false

		p.Parent.Character.UpperTorso.Velocity = Vector3.new(50, 50, 50) * direction
		wait(1)
		script.Parent.CanDamage.Value = true
	end
	
	
end)

i am relatively new with scripting

what i want is that the player that touches the blade gets thrown away to the direction you are facing

any things that i can do?

1 answer

Alright. Let me enumerate everything wrong in this script:

  • The direction variable should be within the Touched function to get the player's torso position for each hit.

  • In a LocalScript, you shouldn't directly get the Character value from LocalPlayer, because that property is null during a small lapse of time when the player just spawned. Use CharacterAdded:wait() to fix that.

  • The Touched function is not only applied when a player touched the part, but when another part collides too. If your tool touched a part, It should look for an humanoid first to check if the touched part is from a character or an NPC, or else, It will return an error for searching for an inexistent Torso.

  • p.Parent.Character.UpperTorso is wrong. It's p.Parent.UpperTorso

  • You're mixing R6 and R15 player models. In the first line you try to get the Torso part (from R6), and in line 9 you're getting UpperTorso.

  • Not necessarily an error, but you should get HumanoidRootPart for direction and pushing instead of Torso, just for more accuracy (and works for both R6 and R15).

So this should be the result:

local player = game:GetService("Players").LocalPlayer
local char = player.CharacterAdded:wait()

script.Parent.Blade.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
	if humanoid and script.Parent.CanDamage.Value then
		
		local direction = char.HumanoidRootPart.CFrame.lookVector * 100
		
		hit.Parent.HumanoidRootPart.Velocity = direction
		wait(1)
		script.Parent.CanDamage.Value = true
		
	end 
end)

By the way, we recomend you to copy and paste the output of the script when you ask for help here, so we find easily the source of the problem.

(Sorry for my english)

This discussion is preserved as it was. New answers, comments, and votes are closed.