Tweening error occurring on players death

Keep getting this error on a tweening line when player dies. Don’t get errors any other time I tween, only on this line
Error on the tweening line
[Bar]
[15:42:13.470 - Can only tween objects in the workspace]

local function updateData(data)
	Coins.Text = data.Coins
	
	CurrentLevel.Text = data.Level
	ExpBack.Text = data.Exp .. ' / ' .. data.ExpNeeded
	ExpFront.Text = ExpBack.Text
	print(Bar) -- Returns the Bar frame
    if Bar then
     	Bar:TweenSize(UDim2.new(data.Exp / data.ExpNeeded, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.25, true)
    end
end

DataUpdated.OnClientEvent:Connect(updateData)
DataUpdated:FireServer()
1 Like

That error happens when you try to tween objects that are parented to nil. Just make sure your GUI is in PlayerGui before you try to tween it.

Is your GUI set to ResetOnSpawn? That could be causing the issue.

Change this:

if Bar then
 	Bar:TweenSize(UDim2.new(data.Exp / data.ExpNeeded, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.25, true)
end

to:

if Bar and Bar.Parent then
 	Bar:TweenSize(UDim2.new(data.Exp / data.ExpNeeded, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.25, true)
end
1 Like

Yes, kinda has to be like that tho, otherwise I’d have to have every single script inside it get the new character

Did @serverModule’s suggestion make any change?

That’s why TweenService is better than these weird GuiObject methods. Tweens will not present this kind of error as they are halted and cleared when no longer required or the associated instance is gone. As for GuiObject tweens, the tween attempts to continue running even after the instance is removed.