Issue with knife throwing system

I’ve edited it into the main post. The lines commented out are my failed attempts at using something else, help would be appreciated on those too.

1 Like

Maybe change the tweening time longer? I think tweening is fine but it ignores collisions. I would recommend using VectorForce instead.

1 Like

When I used tween, I got this error:
20:01:48.385 TweenService:Create property named ‘CFrame’ cannot be tweened due to type mismatch (property is a ‘CoordinateFrame’, but given type is ‘Vector3’) - Server - KnifeServer:51

Line 51 is local tween = ts:Create(kclone, tweeninfo, prop)

My new code, if you need it (it’s just the old, except the AssemblyLinearVelocity part is commented out and the tween stuff is un-commented out.

Code
local k = script.Parent
local handle = k:WaitForChild("Handle")
local ts = game:GetService("TweenService")

local throwevent = game.ReplicatedStorage.Events:WaitForChild("KnifeThrow")
local throwanim = script:WaitForChild("ThrowAnimation")

local db = 2
local isdb = false

local readytime = 0.55

throwevent.OnServerEvent:Connect(function(plr, mouseHit)
	print("recieved")
	local char = plr.Character

	if not char or not char:FindFirstChild("Humanoid") then return end
	
	if isdb then return end
	isdb = true
	
	char.Humanoid:LoadAnimation(throwanim):Play()
	
	wait(readytime)
	
	local kclone = handle:Clone()
	


--	kclone.Velocity = mouseHit.LookVector * 300
	
	--kclone.AssemblyLinearVelocity = CFrame.lookAt(kclone.Position, mouseHit) * 100
	--kclone.AssemblyLinearVelocity = CFrame.lookAt(kclone.Position, mouseHit) * Vector3.new(100,0,0)
	kclone.CanCollide = true
	kclone.Parent = workspace
	handle.Transparency = 1
	
--	kclone.CFrame = CFrame.new(kclone.Position, mouseHit.LookVector * 300)
	
--	local bav = Instance.new("BodyAngularVelocity")
--	bav.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	
--	bav.AngularVelocity = kclone.CFrame:VectorToWorldSpace(Vector3.new(-400,0,0))
--	bav.Parent = kclone
	
	--	game.ReplicatedStorage.Events.KnifeThrow:FireAllClients(kclone, k.Parent)
	
	local tweeninfo = TweenInfo.new(.1, Enum.EasingStyle.Linear)
	local prop = {CFrame = mouseHit}
	
	local tween = ts:Create(kclone, tweeninfo, prop)
	tween:Play()
	
	
--	kclone.CFrame = CFrame.lookAt(kclone.Position, mouseHit)
	--kclone.AssemblyLinearVelocity = kclone.CFrame.LookVector * 200
	
	kclone.Touched:Connect(function(touched)
		if touched.Transparency < 1 and  not k.Parent:IsAncestorOf(touched) then
			
			kclone.Anchored = true
			kclone.CanCollide = false
			
			local hum = touched.Parent:FindFirstChild("Humanoid") or touched.Parent.Parent:FindFirstChild("Humanoid")
			
			if hum then
				hum.Parent:BreakJoints()
			end
			

			
			wait(2)
			kclone:Destroy()
			
		end
	end)
	
	wait(db - readytime)
	isdb = false
	handle.Transparency = 0
end)
	

Could you help me out on the tween part? Thanks!

I think you should remove the {CFrame = mouseHit} and just do mouseHit to fix the error. your creating a CFrame but you also need to add the orientation if your doing so, which your just giving it the Vector3 position. I think changing it to mouseHit will fix the error.

Would that make it just Vector3? I want the rotation to work, if that’s possible.

Changing it to just mouseHit gives the error 'Unable to cast to dictionary. ’

Try {CFrame = CFrame.new(mouseHit)}

Thank you! I have a question though, how does that change it?

Also, if it’s not too much, could you help me on how to make it spin while it’s being thrown?

(Sorry for another question but - since this is a tween, would it go faster the longer the distance?)

Before, you were setting the cframe to mouseHit, which is actually a vector3. All you had to do was create a new cframe from this vector3 for it to work.

quote: why lag the server if you can lag the client instead. fun later performance number 1

function throw(mouse, part)
  local hit = mouse.hit
  local lv = hit.LookVector
  local Mover = Instance.new('BodyVelocity', part)
  Mover.Velocity = lv
end

Yes it would go faster the longer the distance. You could make the length of the tween dependant on the distance between the player and mouseHit for a constant speed.

Unfortunately I’m not sure how to do that, could you give me some pointers?

To calculate the distance between the player and the mouse cursor, you would substract the positions and find the magnitude. Then, you would make the tween length the distance divided by the speed, in studs per second. This would look like

local distance = (char.HumanoidRootPart.Position - mouseHit).Magnitude -- distance between player and mouseHit
local speed = 10 -- studs per second
local tweeninfo = TweenInfo.new(distance / speed, Enum.EasingStyle.Linear) -- tween length dependent on distance and speed
3 Likes

Raycasting is the best solution I have personally used.

That’s making the knife really buggy - is there a way to fix that?

@Galactiq how could I apply raycasting to this?

My knife system may be a little more advanced, however raycasting is how I determine hit objects while the knife is in motion. There is a ton of math involved in my system in particular, but it continuously raycasts short distances to see if it has hit something, and then stops based on that hit.

Whether you choose this route or another, I would recommend replicating the knife info to the client to handle any motion-based functions, as it would eliminate this issue:

Handle any killing or whatever on the server, but send info to each client in order to have smooth, seamless knife motion.

I don’t think I’m quite ready for that yet. Perhaps you could help me out with tweening the knife?

How would I fix the tween time issue? - Help and Feedback / Scripting Support - DevForum | Roblox

Sorry for bump…

But could you further explain what you’ve done with your knife? I’m persoanlly making my own system and I can’t seem to get it right w/ tweenservice or AssemblyLinearVelocity.

How’d you do it? And how could you use raycasting? I’m a bit confused on what you said.

I’ve gotten mine to work with tweenService, but it looks TERRIBLE.
I’m currently following a different tutorial, and it seems promising.

That’s episode 2, but you can go to the creator’s channel to find the other episode.

I did follow that tutorial, but it is quite outdated.

The only problem about it (once you update it) is that it looks unsmooth.

1 Like