Tweening doesn't always fire

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m currently trying to make a bullet trail, that tweens away after (I think) .2 seconds. Seems simple, and that’s what I thought until I ran into this:

It may be hard to see, but the tween is inconsistent, and happens on some clicks and not on others. In this example I was spam clicking to “simulate” a machine gun if I were to make this into one (for testing purposes).

I’ve tried quite a few things to fix this. I’ve lowered the time it takes to tween, I’ve moved the tween out of a function (thinking maybe that slows it down/breaks it) but I’m not sure what else to do, rather than ask for help here.

Here’s the important code!



	if raycastResult then
		print("Hit something")
		local hitpart = raycastResult.Instance
		print(hitpart.Name)
		
		
		
		local distance = (rayOrigin - raycastResult.Position).Magnitude

		p.Anchored = true
		p.CanCollide = false
		p.Transparency = .3
		p.Parent = game.Workspace
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(rayOrigin, raycastResult.Position)*CFrame.new(0, 0, -distance/2)
		Tween:Play()

		print("Finished")

		--CreateVisibleRay(true, rayOrigin, raycastResult, rayDirection, rayOrigin)
		
		if hitpart.Parent:FindFirstChild("Humanoid") then
			print("Found one!")
		else 
			print("No humanoid")
		end
		
	else
		--CreateVisibleRay(false, rayOrigin, raycastResult, rayDirection, rayOrigin)

		local distance = (rayOrigin - rayDirection).Magnitude

		p.Anchored = true
		p.CanCollide = false
		p.Transparency = .3
		p.Parent = game.Workspace
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(rayOrigin, rayDirection)*CFrame.new(0, 0, -distance/2)
		Tween:Play()

		print("Finished")
	end
	

for some context, I’m using a tool for activation, then a remote event to tell the server to do all of the stuff above (plus some unneeded code for this question).

Thanks in advanced if you can help!

2 Likes

Are you using the same trail for the tween? Maybe the tween doesn’t finish before it is called again causing it to break. Consider making a new tween/part each time it’s fired?

1 Like

I agree, if you try to apply more than one tween of the same property to a part then the tween breaks and might not move in the direction you want it to.
Read a bit about this here: TweenBase | Roblox Creator Documentation

1 Like

Actually I think I left it out but here’s the variable


local p = instance.new("Part)

(Or something like that)

I took yours and @awesomefolsom 's advice and I have a function to check if the tween is done. if it is is does p:Destroy(), however I am still noticing that it’s inconsistent
(Less than before but still noticeable)

Kind of confused about what you mean. Would you be comfortable sharing the code, or at least the part messing up?

1 Like

Any errors in console?? and Tween isnt defined by anything or did i miss it?

1 Like

Try this instead of using CFrame:

local Train = game.Workspace.Train
Train:TweenPosition(Vector3.new(-0.75, 0.5, -487.99), "In", "Back", 5, false)
1 Like

:TweenPosition can only be used on GUI Objects like TextLabels or Frames.

2 Likes

Here’s a better look into it @loror



-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-- Creates a visible ray, duhhh

local function CreateVisibleRay (Resultornot, rayOrigin, raycastResult, rayDirection, rayOrigin)
	local p = Instance.new("Part")
	local Info = TweenInfo.new(1)
	local Tween = game:GetService("TweenService"):Create(p,Info,{Transparency=1})
			if Resultornot == true then
				local distance = (rayOrigin - raycastResult.Position).Magnitude
				
				p.Anchored = true
				p.CanCollide = false
				p.Transparency = .3
				p.Parent = game.Workspace
				p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(rayOrigin, raycastResult.Position)*CFrame.new(0, 0, -distance/2)
		
		Tween:Play()
		Tween.Completed:Connect(function(playbackState)
			print("Done")
			p:Destroy()
		end)

		
				print("Finished")
			else
				local distance = (rayOrigin - rayDirection).Magnitude
				
				p.Anchored = true
				p.CanCollide = false
				p.Transparency = .3
				p.Parent = game.Workspace
				p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(rayOrigin, rayDirection)*CFrame.new(0, 0, -distance/2)
	
		Tween:Play()
		Tween.Completed:Connect(function(playbackState)
			print("Done")
			p:Destroy()
		end)
				
				print("Finished")
			end
end



------------- some irrelevant code ------


--- the function that calls the tweening--
	
	if raycastResult then
		print("Hit something")
		local hitpart = raycastResult.Instance
		print(hitpart.Name)
		
		
		
	
		CreateVisibleRay(true, rayOrigin, raycastResult, rayDirection, rayOrigin)
		
		if hitpart.Parent:FindFirstChild("Humanoid") then
			print("Found one!")
		else 
			print("No humanoid")
		end
		
	else
		CreateVisibleRay(false, rayOrigin, raycastResult, rayDirection, rayOrigin)

	
	end
	

@Zach_DaQuack

Yeah, here’s the thing i’m just tweening the transparency as it’s a bullet trail


@JS_Coded

There were no errors, and also see the code above, I added the part where tween is defined. It was late so I accidentally forgot to include the important variables :man_facepalming: