i have a problem with my throwing knife. when my character animation has finished swinging the knife it should fly towards the target but there’s a problem. there’s a slight delay before the knife moves off to the intended target which is bugging me. i appreciate any help ty!!
local function knife_collision(collisionValue, transparencyValue)
for _, part in pairs(knife:GetChildren()) do
if part:IsA("MeshPart") then
part.CanCollide = collisionValue
part.Transparency = transparencyValue
end
end
end
knife_remote.OnServerEvent:Connect(function(p, state, mouse_pos)
local knife_model = game.ReplicatedStorage:WaitForChild("knife_model")
local knife_clone = knife_model:Clone()
local vf:VectorForce = knife_clone.PrimaryPart.VectorForce
local av:AngularVelocity = knife_clone.Handle.AngularVelocity
if state == "makeknife_clone" then
knife_clone.Parent = workspace
knife_collision(false, 1)
knife_clone:PivotTo(knife.PrimaryPart.CFrame)
local knifeclone_pos = knife_clone.PrimaryPart.Position
local direction = (mouse_pos - knifeclone_pos).Unit
local throw_speed = 50
vf.Force = Vector3.new(0, workspace.Gravity * knife_clone.PrimaryPart.AssemblyMass, 0)
knife_clone.PrimaryPart.CFrame = CFrame.new(knife_clone.PrimaryPart.Position, mouse_pos) * CFrame.Angles(0, math.rad(180),0)
knife_clone.PrimaryPart.AssemblyLinearVelocity = direction * throw_speed
av.AngularVelocity = knife_clone.PrimaryPart.CFrame:VectorToWorldSpace(Vector3.new(throw_speed / 8,0,0))
av.MaxTorque = 1000
debrisService:AddItem(knife_clone, 5)
elseif state == "makeknife_visible" then
knife_collision(true, 0)
end
end)
The delay occurs because you send the server a request to throw the knife and wait for its response.
Instead, throw the knife from client already and send this to server to make it replicate for other clients by doing a FireAllClients except for the player who requested.
Register hits from client and send this to server. Then, calculate whether such a hit is actually possible by checking the time that has passed since throwing the knife and calculating the distance it should have travelled.
There’s a bit of a delay from when the client sends a request to the server.
What you can do is make the knife still be thrown on the server but create one just for effect on the client side, so it looks seamless when the knife is thrown.
You can then just show the one that the server threw to the other clients.
i did what you suggested right. so i sent the position, direction and throw speed to the server so that i could fire it to all clients and clone it on the client but it only clones for my client and not others.
i did some debugging and it only prints my name.
server:
knife_remote.OnServerEvent:Connect(function(p, state, knifeclone_pos, direction, throw_speed)
local c = p.Character
if state == "knife_thrown" then
knife_collision(false, 1, c)
knife_remote:FireAllClients(p, knifeclone_pos, direction, throw_speed)
elseif state == "makeknife_visible" then
knife_collision(true, 0, c)
end
end)
client:
knife_remote.OnClientEvent:Connect(function(player, knifeclone_pos, direction, throw_speed)
print("Event received for player:", player.Name) -- Debug check
local knife_clone = game.ReplicatedStorage:WaitForChild("knife_model"):Clone()
local vf = knife_clone.PrimaryPart:FindFirstChild("VectorForce")
local av = knife_clone.Handle:FindFirstChild("AngularVelocity")
knife_clone.Parent = workspace
knife_clone:PivotTo(knifeclone_pos)
if vf then
vf.Force = Vector3.new(0, workspace.Gravity * knife_clone.PrimaryPart.AssemblyMass, 0)
end
if av then
av.AngularVelocity = knife_clone.PrimaryPart.CFrame:VectorToWorldSpace(Vector3.new(throw_speed / 8, 0, 0))
av.MaxTorque = 1000
end
knife_clone.PrimaryPart.AssemblyLinearVelocity = direction * throw_speed
game:GetService("Debris"):AddItem(knife_clone, 5)
end)
o nvm i got it. ty guys sm for ur help. this post was a huge help too. i stored my remote event in the tool and the server script in the tool. had to put the remote in replicatedstorage and the serverscript in srvrscriptservice