#Attributes of a Part for Clones for Health Value
1 messages · Page 1 of 1 (latest)
archivable=true?
yes
one sec sorry lmfao
here is my code for spawning the object clone: if result then
local Object= Workspace.Object
local Objects = Object:Clone()
Objects.Position = Vector3.new((result.Position.X), result.Position.Y, result.Position.Z)
Objects.Parent = spawnedObjects
Objects.Anchored = true
end
wait(10)
local Object = script.Parent
Object:SetAttribute("Health", 100)
local UserDamage = 10
local MaxHealth = 100
local Front = workspace.Object.HealthUI.Front
fullSize = Front.Size
Front.Size= UDim2.new(fullSize.X.Scale, fullSize.X.Offset, fullSize.Y.Scale, fullSize.Y.Offset)
-- when object is clicked, it's health decreases by 10 (or by specific user damage for future reference)
local clickDetector = game.Workspace:WaitForChild("Object"):WaitForChild("ClickDetector")
clickDetector.MouseClick:Connect(function(player)
Health.Value -= UserDamage
if Object.Attributes.Health <= 0 then
workspace.Object:Destroy()
end
end)
-- when health changes, one health UI frame shrinks
local function onHealthChanged()
local percentage = math.clamp(Health / MaxHealth, 0,1)
Front.Size = UDim2.new(fullSize.X.Scale, fullSize.X.Offset - (fullSize.X.Offset * percentage), fullSize.Y.Scale, fullSize.Y.Offset)
end
Health.Changed:Connect(onHealthChanged)
onHealthChanged()
print(Health.Value)
--this is the code for health changing, but not sure if it is correct anymore because i replaced number value with attributes (probably where the error is)
uh
where did you ever define health
also plz wrap the code like this
```lua
print("Hello World!")
```
it formats it
ohhh sorry
i copy & pasted the code after messing with it for a bit so its not the og
Object:SetAttribute("Health", 100)
this code is so messy, i think im going to start over from scratch
but just to say, the overall goal was to clone an object that had a healthui, and i wanted each clone to have its own health ui idk idk
okay so that doesn't automaticlaly create a health variable
you have to do ```lua
Object:GetAttributeChangedSignal("Health"):Connect(function()...
and
Object:GetAttribute("Health")
Object:SetAttribute("Health", number)
its kinda confusing
it would help me to understand if we went step by step (if u dont mind)
** You are now Level 3! **
first, we establish instances like maxHealth and the frame of the UI thats moving
then, make a clickDetector function for the cloned objects and establish attribute health using Object:GetAttribute("Health") Object:SetAttribute("Health", number)
this click detector changes the attribute health using lua Object:GetAttributeChangedSignal("Health"):Connect(function()...
and then a previously defined function will change the UI frame using Health Attribute value (percentage of Health remaining really)
this is how im understanding it, i'll rewrite my code like this pray for me lol
oh wait, can i still just say Health.Value to be able to manipulate its value