How to fix my knife client script and server script?

here is the local script for my knife the throwing doesn’t work it clones sword but doesn’t throw it it just makes the clone fall into the ground:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local throwEvent = ReplicatedStorage:WaitForChild("ThrowKnifeEvent")
uis = game:GetService("UserInputService")
knife = script.Parent

-- Function to handle mouse input for throwing
local function throwKnife()
	local mouse = game.Players.LocalPlayer:GetMouse()
	local targetPosition = mouse.Hit.Position
	local cloned = knife.Mesh:Clone()
	cloned.Parent = workspace
	local bodyang = Instance.new("LinearVelocity", cloned)
    
	cloned.Velocity = knife.CFrame.LookVector * 100

	throwEvent:FireServer(targetPosition)
end

-- Example: Throw knife when 'E' key is pressed
uis.InputBegan:Connect(function(inp, gpe)
	if inp.KeyCode == Enum.KeyCode.E then
		throwKnife()
	end
end)

and serverscript doesn’t work from leg to head:

local Knife = script.Parent

-- Function to handle knife throwing
local function throwKnife(player, targetPosition)
	local knifeClone = Knife:Clone()
	knifeClone.Parent = workspace

	-- Set initial position and orientation
	knifeClone.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position, targetPosition)

	knifeClone.Touched:Connect(function(hit)
		if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
			-- Handle damage or hit effects here
			hit.Parent.Humanoid:TakeDamage(50)
			knifeClone:Destroy()
		end
	end)
end

-- RemoteEvent to receive throw requests from clients
local throwEvent = Instance.new("RemoteEvent")
throwEvent.Name = "ThrowKnifeEvent"
throwEvent.Parent = game.ReplicatedStorage

-- Handle throw requests from clients
throwEvent.OnServerEvent:Connect(function(player, targetPosition)
	throwKnife(player, targetPosition)
end)

even hitting doesn’t work please fix it i need the help

4 Likes

It could be that your moving the knife on the client, and your cloning the knife on the server and checking if it hit something but it didn’t move on the server.

2 Likes

@RedOver133 I’m scripting a new one but what’s the: local knifeClone = Knife:Clone() knifeClone.Parent = workspace

2 Likes

@RedOver133 I haven’t tested but here is a script:

----[[ CLIENT ]]----
local Players = game:GetService(“Players”)
local Mouse = Players.LocalPlayer:GetMouse()

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local UserInputService = game:GetService(“UserInputService”)

local ThrowKnifeEvent = ReplicatedStorage:WaitForChild(“ThrowKnifeEvent”)

local Knife = script.Parent
local Keybind = Enum.KeyCode.E --// Change to your likings

local function ThrowKnife()

local HitPosition = Mouse.Hit.Position
ThrowKnifeEvent:FireServer(Knife, HitPosition)

end

UserInputService.InputBegan:Connect(function(Input, Gui)

if Gui then return end --// If clicked on chat menu or something else

if Input.KeyCode == Keybind then
	
	ThrowKnife()
	
end

end)

----[[ Server ]]----
local Knife = script.Parent

local ThrowEvent = Instance.new(“RemoteEvent”)
ThrowEvent.Name = “ThrowKnifeEvent”
ThrowEvent.Parent = game.ReplicatedStorage

local function ThrowKnife(Player, TargetPosition)

local Cloned = Knife.Mesh:Clone()
Cloned.Parent = workspace
Cloned.CFrame = CFrame.new(Player.Character.HumanoidRootPart.Position, TargetPosition)

local LinearVelocity = Instance.new("LinearVelocity", Cloned)
Cloned.Velocity = Cloned.CFrame.LookVector * 250

Cloned.Touched:Connect(function(Hit)
	
	if not Hit:IsDescendantOf(Player.Character) then
		
		if Hit.Parent:FindFirstChild("Humanoid") then
			
			Hit.Parent.Humanoid.Health = 0
			
		end
		
	end
	
end)

end

ThrowEvent.OnServerEvent:Connect(function(Player, TargetPosition)

ThrowKnife(Player, TargetPosition)

end)

1 Like

I would recommend to make the remote event in the replicated storage, and not by adding it in the script

1 Like

no i cloned the knife in workspace

1 Like

it actually is in it tho idk how to fix the issue

1 Like

Remove the local throwEvent = Instance.new("RemoteEvent") and put it in the replicated storage instead

1 Like

Would my script even work? @YourPlxDev

1 Like

I haven’t tested it but I assume that it could possibly…

1 Like

nope didn’t work doesn’t do anything but i respect ur attempt

1 Like

I would recommend saying this instead:

local throwEvent = game.ReplicatedStorage:WaitForChild("ThrowKnifeEvent")

1 Like

deleted it tho idk the issue :frowning:

1 Like

What exactly did you delete? In the replicatedstorage or in script?

1 Like

script i deleted the instance.new and replaced it with the line you told me still doesn’t work

1 Like

did you put throwEvent.OnServerEvent?

1 Like

Did you add the remote event into the ReplicatedStorage?

1 Like

everything is on its place but still doesn’t work :frowning:

1 Like

Does it print out errors? Check the output

1 Like

no errors no warnings empty like my brain

1 Like