#Attributes of a Part for Clones for Health Value

1 messages · Page 1 of 1 (latest)

gleaming cedar
#

i am having trouble on how to make the script of an object applicable to its clones. been trying since earlier to no avail. initally used an number value for health but changed it to an attribute. still getting errors : Workspace.Object.HealthScript:35: attempt to index nil with 'Changed'

dense nova
red pollen
#

archivable=true?

gleaming cedar
#

yes

gleaming cedar
#

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)

dense nova
#

where did you ever define health

#

also plz wrap the code like this
```lua
print("Hello World!")
```

#

it formats it

gleaming cedar
#

ohhh sorry

#

i copy & pasted the code after messing with it for a bit so its not the og

gleaming cedar
#

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

dense nova
#

you have to do ```lua
Object:GetAttributeChangedSignal("Health"):Connect(function()...

#

and

Object:GetAttribute("Health")
Object:SetAttribute("Health", number)
#

its kinda confusing

gleaming cedar
#

it would help me to understand if we went step by step (if u dont mind)

rose raftBOT
#

studio** You are now Level 3! **studio

gleaming cedar
#

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