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)