Tweening Function Doesn't Work

This should cause a UI animation to play when they first join.
https://gyazo.com/ace9feb948b5cc8cf3eea2d70bf51c96

This one below works.

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local btn = script.Parent

local function CharacterAdded()
	local goal = {}
	goal.Position = UDim2.fromScale(0.5,0.682)

	local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Bounce)
	local tween = TweenService:Create(btn, tweenInfo, goal)
	tween:Play()
end

local function PlayerAdded()
	if char then
		CharacterAdded()
	end
end
Players.PlayerAdded:Connect(PlayerAdded())

This one below doesn’t work; it doesn’t error, the function just doesn’t get triggered for whatever reason.

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local btn = script.Parent

Players.PlayerAdded:Connect(function()
	if char then
		local goal = {}
		goal.Position = UDim2.fromScale(0.5,0.682)

		local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Bounce)
		local tween = TweenService:Create(btn, tweenInfo, goal)
		tween:Play()
	end
end)

I’m not really sure why this could be. Does anyone have any idea?

1 Like

Where is char?

1 Like

Where is btn being defined?

It’s the scripts parent, I forgot to add it my bad. (It’s the image button)

I forgot to include it, I edited and added it.

Have you tried using TweenPosition?

btn:TweenPosition(UDim2.fromScale(0.5,0.682), "In", "Bounce", 1, false)
1 Like

is it a LocalScript? If so, I might have a fix.

Yes it is a localscript. -----

It still isn’t working. The problem isn’t the Tweening itself, it’s that the function will not fire when it’s being defined while also connecting to the event. (The second code I posted)
The tweening works fine in the first code.

Players.PlayerAdded:Connect(function()

Why are you using PlayerAdded?

Dont you aready have access to the Player?

Put it inside StarterPlayerScripts

then instead of

Players.PlayerAdded:Connect(function()
	if char then

just do

Players.LocalPlayer.CharacterAdded:Connect(function()

I’m using PlayerAdded and CharacterAdded so the player has loaded before the tween starts

Yeah this didn’t work either. No error, the function just doesn’t run.

LocalScripts apply when the Player is already added into the game, unless you want to apply something to the Player or Char, its unessecary.

Oh ok, but I don’t think that’s why the code doesn’t work. The first one also uses PlayerAdded and it works fine.

try this then

repeat wait(1) until game:IsLoaded()
		local goal = {}
		goal.Position = UDim2.fromScale(0.5,0.682)

		local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Bounce)
		local tween = TweenService:Create(btn, tweenInfo, goal)
		tween:Play()

Yeah this doesn’t work either. I don’t want to rewrite the code completely, I’m only just confused why one works but the other doesn’t when they’re both doing the same thing just in a different syntax.

1 Like

Welp, I don’t really know how to help you then :man_shrugging:

Use prints to make sure the if char then is actually true.

Instead of creating the property table this way:

Try this way:

local goal = { 	Position = UDim2.fromScale(0.5,0.682) }

To be safe, define all of the arguments in TweenInfo.new()

You should do plr.CharacterAdded:Wait(), make the wait() uppercase.

And also, I see this common mistake with beginner developers. When using :Connect(), You don’t use the parentheses. You just REFERENCE the function. Don’t call it. Example:

Players.PlayerAdded:Connect(PlayerAdded) -- good
Players.PlayerAdded:Connect(PlayerAdded()) -- bad, this won't work.

Again, it’s bad practice to name a function the same as an event.

1 Like

Ok this is what I have now

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local btn = script.Parent


Players.PlayerAdded:Connect(function()
	if char then
		local goal = { 	Position = UDim2.fromScale(0.5,0.682) }

		local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out)
		local tween = TweenService:Create(btn, tweenInfo, goal)
		tween:Play()
	end
end)

Also like you said, I think the reason the first code was “working” was because it was calling the function when it was supposed to be “connecting” to the event. But now that I fixed it and removed the parenthesis, I can see the entire event is not connecting to the function.
I included prints right above the if statement at the beginning of the function, and one inside the if statement and got nothing in the output.