Latency when TweenSize on Frame

So I think the video could be much explanatory, bit i am currently trying to recreate a dynamic crosshair in 2d that would tween as the player shoots. I have this script.
Code :

local mouse = game.Players.LocalPlayer:GetMouse()
local Cross = script.Parent.Frame


local X = 0.002
local Y = 0.005

-- {0.002, 0},{0.005, 0} <- Default

local Shooting = false
local mousedown = false
coroutine.wrap(function()
	mouse.Button1Down:Connect(function()
		mousedown = true
	end)

	mouse.Button1Up:Connect(function()
		mousedown = false
	end)
end)()

game["Run Service"].RenderStepped:Connect(function()
	local CrossSizeX = Cross.Size.X.Scale
	local CrossSizeY = Cross.Size.Y.Scale
	
	if mousedown then
		Cross:TweenSize(UDim2.fromScale(CrossSizeX + X, CrossSizeY + Y), "In", "Linear", .9)
		X += 0.004
		Y += 0.01
	else    
		Cross:TweenSize(UDim2.new(0.002, 0, 0.005, 0), "In", "Linear", .8)
	end
end)

Video :


Thanks !

Might be because you’re checking repeatedly using RenderStep and there is a big margin of error for tween conflicts. I would suggest just using your coroutine function and adding the tween:Play() there for when the player shoots their gun, and a check to see if the current tween is still playing to avoid conflicts.

I don’t see how i could do that ? Sorry for late response i was busy outside.

Try this way instead. It just makes the tween go out and back in like a “bounce” in 1 go. Also make sure the image being tweened has an AnchorPoint of (0.5, 0.5) so it tweens from the center.

--Services
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")

--Constants
local MOUSE_CLICK = Enum.UserInputType.MouseButton1

--Variables
local mouse = game.Players.LocalPlayer:GetMouse()
local Cross = script.Parent.Frame

--The X and Y represent how large it will grow
local X = 0.1
local Y = 0.1

--How long it will take to tween
local time = 0.1

local CrossSizeX = Cross.Size.X.Scale
local CrossSizeY = Cross.Size.Y.Scale

local tweenInfo = TweenInfo.new(time, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true)
local buttonEffect = TweenService:Create(Cross, tweenInfo, {Size = UDim2.fromScale(CrossSizeX + X, CrossSizeY + Y)})

--Function to tween scope
local function TweenScope(actionName, inputState, inputObj)
    buttonEffect:Play()
end

--\\important information below//--

--CALL THIS TO START TRACKING IF MOUSEBUTTON1 IS BEING CLICKED
ContextActionService:BindAction("TweenScope", TweenScope, false, MOUSE_CLICK)

--CALL UNBIND WHEN GUN IS UNEQUIP TO UNBIND THE ACTION, REFERENCING THE NAME OF THE BINDACTION
--PUT THIS WHERE YOU UNEQUIP YOUR GUN OR ELSE IT WILL OVERRIDE THE FUNCTION AND WONT WORK
ContextActionService:UnbindAction("TweenScope")

Does it makes it grow as the player keeps MouseButton1 ?

At the moment no, but thats pretty trivial to do. Do you know how big you want it to grow? And is the gun automatic or semi fire? Maybe try implementing the code above first and then see if you can figure that part out.

Its an automatic weapon, I will try your script but for the moment i don’t see how i could make this

Something like below works. I’m essentially just increasing the size in the heartbeat, then resetting it back to normal at the end. If you have any questions feel free to ask me.

Video of it working:

Code:

--Services
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

--Constants
local MOUSE_CLICK = Enum.UserInputType.MouseButton1
local TIME = 0.2

--Variables
local mouse = game.Players.LocalPlayer:GetMouse()
local Cross = script.Parent
--local Cross = script.Parent.Frame

local X = 0.1
local Y = 0.1

local start = tick()
local nextStep = start + TIME
local iter = 1

local CrossSizeX = Cross.Size.X.Scale
local CrossSizeY = Cross.Size.Y.Scale

local tweenInfo = TweenInfo.new(TIME/2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true)
local tweenInfo2 = TweenInfo.new(TIME, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false)
local returnToNormal = TweenService:Create(Cross, tweenInfo2, {Size = UDim2.fromScale(CrossSizeX, CrossSizeY)})

--Function to tween scope
local function TweenScope(actionName, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		TweenConnection = RunService.Heartbeat:Connect(function(dt)
			if (tick() >= nextStep) then
				iter = iter + 1
				nextStep = start + (iter * TIME)
				
				local buttonEffect = TweenService:Create(Cross, tweenInfo, {Size = UDim2.fromScale(CrossSizeX + X, CrossSizeY + Y)})
				buttonEffect:Play()
				
				--X and Y size increases
				X += 0.03
				Y += 0.03

			end
		end)

	elseif inputState == Enum.UserInputState.End then
		TweenConnection:Disconnect()
		returnToNormal:Play()
		X = 0.1
		Y = 0.1
	end
end

ContextActionService:BindAction("TweenScope", TweenScope, false, MOUSE_CLICK)
1 Like