How would I fix the tween time issue?

I’ve been trying to create a throwable knife (for a while now), but I’ve been having issues. I tried using velocity, but that had issues.

Now I’m using tween, but I’m having 2 issues - I don’t know to how make the knife rotate so that it isn’t really weird, and how to make it so that the speed doesn’t increase depending on the distance. The main problem is the speed part.

It’d also be helpful if anyone else had another solution.

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 clones = 0

local readytime = 0.7


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)
	
	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)
	
	
	--kclone.Velocity = (script.Parent.Parent:FindFirstChild("Humanoid").TargetPoint - kclone.CFrame.p).unit * 100
	-- set the orientation to the direction it is being thrown in
--	kclone.CFrame = CFrame.new(kclone.CFrame.p, kclone.CFrame.p + kclone.Velocity) * CFrame.Angles(0, 0, math.rad(-90))
	--local floatingForce = Instance.new('BodyForce', kclone)
--	floatingForce.force = Vector3.new(0, 196.2 * kclone:GetMass() * 0.98, 0)
	
	local function DecideTweenTime(mag)
		local tweeninfo = TweenInfo.new(mag / 10)
		return tweeninfo
	end

--	local tweeninfo = DecideTweenTime((char.HumanoidRootPart.Position - mouseHit).magnitude)
	
	
	local tweeninfo = TweenInfo.new(.1, Enum.EasingStyle.Linear)
	local prop = {CFrame = CFrame.new(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)
	

Thanks!

Mess around with CFrame.new() * CFrame.new(math.rad())

I believe there’s a simple fix to this. Since speed depends on time, you should just change the number in your TweenInfo. Like so:

-- Make it go fast
TweenInfo.new(1) 
-- Make it go slower
TweenInfo.new(5)

Sorry if I didn’t explain well. In my script, the tween time is always 0.1, so it goes faster the further the target is from the knife.

Thanks for the tip on rotation though. My main issue was the tween time problem.

Well, I guess you could set up a simple script to decide on the Tween time.

if (char.HumanoidRootPart - HitPos).magnitude > 25 then
  local TInfo = TweenInfo.new(1)
elseif (char.HumanoidRootPart) -- And so on
end

I’m a bit confused here, could you explain your script?

Yesterday I did get help from someone, and they gave me this, I think that would be more efficient:

It makes the knife glitch out however, is there a way to fix that?

(It’s not that efficient, but it might work).

Basically, it’ll decide the time it would take for the tween to play depending on how far the target is. The magnitude of the player’s RootPart - mouse’s hit position (HitPos) (so basically how far they are from each other) will decide the time to play the tween.

Maybe even a function like this:

function DecideTweenTime(mag)
 local TInfo = TweenInfo.new(mag / 10)
 return TInfo
end

local TInfo = DecideTweenTime((char.HumanoidRootPart - MouseHitPos).magnitude)

Giving me the error “Position is not a valid member of Vector3”.

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 clones = 0

local readytime = 0.7

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)
	
	if clones < 1 then
		kclone = handle:Clone()
		clones +=1 
	end

	


--	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 function DecideTweenTime(mag)
		local tweeninfo = TweenInfo.new(mag / 10)
		return tweeninfo
	end

	local tweeninfo = DecideTweenTime((char.HumanoidRootPart - mouseHit.Position).magnitude)
	
	
	--local tweeninfo = TweenInfo.new(.1, Enum.EasingStyle.Linear)
	local prop = {CFrame = CFrame.new(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()
			clones = 0
			
		end
	end)
	
	wait(db - readytime)
	isdb = false
	handle.Transparency = 0
end)
	

It’s happening because you’re putting in a .Position to mouseHit when mouseHit is already the position of your mouse in Vector3

Tried removing already - gives this error:

  Players.RipPBB_TUD.Backpack.Knife2.KnifeServer:58: invalid argument #1 (Vector3 expected, got Instance)

Oh, I got it.
Do this:

local tweeninfo = DecideTweenTime((char.HumanoidRootPart.Position - mouseHit).magnitude)

Unfortunately that’s yielding the same results as the other - the knife is slightly glitching out before moving. That’s why I tried raycasting, perhaps you could help me on that?

Here’s a video:
https://gyazo.com/34e4466da6b35014e8227bb4bb7a40eb

Gotta go now, I’ll get back to you tomorrow.

1 Like

I’m not experienced with Raycasting, but I’ll try to help you.

I tried out raycasting, and it doesn’t help me much. Do you know why the knife is glitching when I try to decide the tween time using code?

If I had to guess, in the video you sent, you’re trying to get the mouse.Hit of the sky. IIRC Mouse.Hit is raycasting to the position of the mouse, so if it doesn’t hit anything pretty sure you’re trying to get the magnitude between your HumanoidRootPart and nil, which is probably why it’s glitching out.

It’s still glitching out no matter what I’m pointing at.
https://gyazo.com/85d5a4a700917d2f396e30876b83be35

You’re tweening the CFrame of the knife, you might be messing with the CFrame depending on how you’re rotating it, not sure.

In that case I have no idea how to fix it, do you have any suggestions how?

Can you provide your current code?

Code (there is a local script firing the remote)
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 clones = 0

local readytime = 0.7


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)
	
	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)
	
	
	--kclone.Velocity = (script.Parent.Parent:FindFirstChild("Humanoid").TargetPoint - kclone.CFrame.p).unit * 100
	-- set the orientation to the direction it is being thrown in
--	kclone.CFrame = CFrame.new(kclone.CFrame.p, kclone.CFrame.p + kclone.Velocity) * CFrame.Angles(0, 0, math.rad(-90))
	--local floatingForce = Instance.new('BodyForce', kclone)
--	floatingForce.force = Vector3.new(0, 196.2 * kclone:GetMass() * 0.98, 0)
	
	local function DecideTweenTime(mag)
		local tweeninfo = TweenInfo.new(mag / 10)
		return tweeninfo
	end

--	local tweeninfo = DecideTweenTime((char.HumanoidRootPart.Position - mouseHit).magnitude)
	
	
	local tweeninfo = TweenInfo.new(.1, Enum.EasingStyle.Linear)
	local prop = {CFrame = CFrame.new(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)
	

I still don’t get why it’s not working - it’s just setting the time of a tween. Shouldn’t (almost) any number value work just fine? When I put .1 as the tween time (as shown in the code above) it works just fine.