"Can only tween objects in the workspace" error

I have a model that can be broken by the player when its health reaches 0. The model also has a health bar billboard gui which tells the player how much health it has left. This model, however, resides in ServerStorage until it is bought by the player, then it is cloned to the workspace. Because it is not originally in the workspace, I get the “Can only tween objects in the workspace” error in the health bar script which is annoying.

Script:

local mainHelthFrame = script.Parent
local mainHealthGui = mainHelthFrame.Parent

local char = mainHealthGui.Parent.Parent

local human = char.Destructible

local healthBar = mainHelthFrame:WaitForChild("HealthBar")
local healthText = mainHelthFrame.healthText
local name = mainHealthGui.name
local maxHealth = human.MaxHealth
local health = human.Health

healthBar:TweenSize(UDim2.new(health/maxHealth, 0, healthBar.Size.Y.Scale, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false) -- error

healthText.Text = health .. "/" .. maxHealth

human.HealthChanged:Connect(function(currentHealth)
	
	maxHealth = human.MaxHealth
	
	healthBar:TweenSize(UDim2.new(currentHealth/maxHealth, 0, healthBar.Size.Y.Scale, 0),  Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false)
	
	healthText.Text = math.round(currentHealth) .. "/" .. maxHealth
	
end)

You can just disable the script until you clone it into workspace.

1 Like

I tried doing that but it didn’t work and was kinda inefficient, is there anything I can do to modify the existing script without disabling it?

1 Like

Well I don’t think you’d need to tween the size of the UI when you can’t see it. Just set the size directly instead of tweening it at the line where it errors.

1 Like

You can see the gui when it’s in the workspace though

1 Like

Yes, but not when it’s in ServerStorage. You’re running healthBar:TweenSize() when the script is created, when it is in ServerStorage. Just change that line to healthBar.Size = UDim2.new(health/maxHealth, 0, healthBar.Size.Y.Scale, 0).

2 Likes

Ohh, I get it now. That makes a lot of sense, thanks!

2 Likes

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