Any tips to improve my knife throw?

local rep = game.ReplicatedStorage

local Deb = game:GetService("Debris")

local anim = script.Throw:Clone()
local hitanim = script.Hit:Clone()

local camera = game.Workspace.CurrentCamera

local players = game:GetService("Players")

rep.Stand.TW.Knife.OnServerEvent:Connect(function(Player, direction)
	wait(0.2)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local stand = Character:WaitForChild("Stand")
	
	local knife = script.Knife1:Clone()
	knife.Parent = Workspace
	knife.CFrame = Character:WaitForChild("HumanoidRootPart").CFrame 
	knife.Orientation = Character:WaitForChild("HumanoidRootPart").Orientation
	knife.Parent = workspace
	
	Deb:AddItem(knife,5)
	
	local Vel = Instance.new("BodyVelocity",knife)
	Vel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	Vel.Velocity = Character:WaitForChild("HumanoidRootPart").CFrame.lookVector * 50 
	

	local ignorelist = {}

	local antiGravity = Instance.new("BodyForce")
	antiGravity.Force = Vector3.new(0, workspace.Gravity * script.Knife1:GetMass(), 0)
	antiGravity.Parent = script.Knife1

	
	
	knife.Touched:Connect(function(hitpart)
		antiGravity:Destroy()
		if not hitpart:IsDescendantOf(Character) then
			if hitpart.Parent:FindFirstChild("Humanoid") then
				local enemy = hitpart.Parent
				local enemys = players:GetPlayerFromCharacter(enemy)
				if (table.find(ignorelist,enemy) == nil) then
					table.insert(ignorelist,enemy)
					enemy.Humanoid:TakeDamage(4)
					enemy.Humanoid:LoadAnimation(hitanim):Play()
					local Blood = script.Blood:Clone()
					Blood.Parent = enemy.UpperTorso
					enemy.Humanoid.WalkSpeed = 5	
					enemys.Backpack.Value.UsingMove.Value = true
					wait(1.25)
					knife:Destroy()
					antiGravity:Destroy()
					Blood:Destroy()
					enemy.Humanoid.WalkSpeed = 16
					enemys.Backpack.Value.UsingMove.Value = false
				end				
			end
		end
	end)
end)

rep.Stand.TW.Knife.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local Stand = Character.Stand
	local AnimControl = Stand:WaitForChild("AnimationController")
	local Sound = script.ThrowSound:Clone()
	Sound.Parent = Stand
	
	if Stand then
		if Player.Backpack.Value.UsingMove.Value == false then
			Player.Backpack.Value.UsingMove.Value = true
			Character.Humanoid:LoadAnimation(anim):Play()
			Sound:Play()
			wait(1.25)
			Player.Backpack.Value.UsingMove.Value = false
		end
	end			
end)

  1. Update to the task libary.
    task.wait, instead of wait.
  2. Use the GetService method always. Do not do game.ReplicatedStorage, use game:GetService(“ReplicatedStorage”), same for Workspace.
  3. Set the Network Ownership of the knife part to the server.
1 Like

Few tips to make your script more stable and reliable:

rep should be local rep = game:GetService("ReplicatedStorage")

rep.Stand.TW.Knife might not exist before the script loads. Do: rep:WaitForChild("Stand"):WaitForChild("TW"):WaitForChild("Knife")

Any usage of game.Workspace should be game:GetService("Workspace") or simply workspace.

Any usage of wait() is sometimes slow and unreliable. Use task.wait() instead.

These variables:

local anim = script.Throw:Clone()
local hitanim = script.Hit:Clone()

should be:

local anim = script:WaitForChild("Throw"):Clone()
local hitanim = script:WaitForChild("Hit"):Clone()

You are also using the now deprecated Humanoid:LoadAnimation(). Use Humanoid.Animator:LoadAnimation().

1 Like