[Solved] Crosshair goes too far than expected using TweenService

  1. What do you want to achieve?
    I want the crosshair to stay at the same position, it started playing the tween.
  2. What is the issue?
    If I shoot the gun too fast, the crosshair goes too far.
  3. What solutions have you tried so far?
    I tried using the tween.Completed function + repeat until. No success.

crosshairAnimation_Remote.OnClientEvent:Connect(function()
	for i,GuiObject in pairs(Crosshair:GetChildren()) do
		if GuiObject:IsA("ImageLabel") then
			local startPosition = GuiObject.Position
			local endPosition = UDim2.new(0, 0, 0, 0)
			local AdjustOffset = 6
			
			if GuiObject.Name == "Top" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset - AdjustOffset)
			elseif GuiObject.Name == "Bottom" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset + AdjustOffset)
			elseif GuiObject.Name == "Right" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset + AdjustOffset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset)
			elseif GuiObject.Name == "Left" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset - AdjustOffset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset)
			end
			
			local tween = tweenService:Create(GuiObject, tweenInfo, {Position = endPosition})
			tween:Play()
			
			tween.Completed:Connect(function(state)
				repeat
					wait()
					GuiObject.Position = startPosition
				until GuiObject.Position == startPosition
			end)
		end
	end
end)

Thanks for any help.

1 Like

Test this changed script:

local animating = false
crosshairAnimation_Remote.OnClientEvent:Connect(function()
	for i,GuiObject in pairs(Crosshair:GetChildren()) do
		if GuiObject:IsA("ImageLabel") and animating == false then
			animating = true
			local startPosition = GuiObject.Position
			local endPosition = UDim2.new(0, 0, 0, 0)
			local AdjustOffset = 6

			if GuiObject.Name == "Top" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset - AdjustOffset)
			elseif GuiObject.Name == "Bottom" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset + AdjustOffset)
			elseif GuiObject.Name == "Right" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset + AdjustOffset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset)
			elseif GuiObject.Name == "Left" then
				endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset - AdjustOffset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset)
			end

			local tween = tweenService:Create(GuiObject, tweenInfo, {Position = endPosition})
			tween:Play()

			tween.Completed:Connect(function(state)
				animating = false
			end)
		end
	end
end)
1 Like

Now only one crosshair object gets animated, instead of all 4.

Oh i forgot, this is the fixed one:

local animating = false
crosshairAnimation_Remote.OnClientEvent:Connect(function()
	if not animating then
		animating = true
		local max = 0
		local finished = 0
		for i,GuiObject in pairs(Crosshair:GetChildren()) do
			if GuiObject:IsA("ImageLabel")then
				max += 1
			end
		end
		for i,GuiObject in pairs(Crosshair:GetChildren()) do
			if GuiObject:IsA("ImageLabel")then
				animating = true
				local startPosition = GuiObject.Position
				local endPosition = UDim2.new(0, 0, 0, 0)
				local AdjustOffset = 6

				if GuiObject.Name == "Top" then
					endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset - AdjustOffset)
				elseif GuiObject.Name == "Bottom" then
					endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset + AdjustOffset)
				elseif GuiObject.Name == "Right" then
					endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset + AdjustOffset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset)
				elseif GuiObject.Name == "Left" then
					endPosition = UDim2.new(GuiObject.Position.X.Scale, GuiObject.Position.X.Offset - AdjustOffset, GuiObject.Position.Y.Scale, GuiObject.Position.Y.Offset)
				end

				local tween = tweenService:Create(GuiObject, tweenInfo, {Position = endPosition})
				tween:Play()

				tween.Completed:Connect(function(state)
					finished += 1
				end)
			end
		end
		repeat wait() until finished==max
		animating = false
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.