Add Property To Object

So i have this script here how can i make a custome property called power to the player

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local PowerBar = script.Parent.PowerBar.Bar


local Stamina = 100
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16




PowerBar.Size = UDim2.new(1, 0, 1, 0)
Character:WaitForChild("Humanoid").Changed:Connect(function(property)
	if property == "Power" then
		PowerBar.Size = UDim2.new(Character.Humanoid.Power / 100, 0, 1, 0)
		while true do
			if not Running and PowerBar < 100 then
				PowerBar = math.min(PowerBar - 1, 100)
				PowerBar:TweenSize(UDim2.new(PowerBar / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
			end
			wait(0.1) 
			print(PowerBar, PowerBar.Size)
		end
	end
end)



The closest thing you can get to custom properties is Attributes.

Here’s a tutorial: Instance Attributes | Documentation - Roblox Creator Hub

even if i cant how can i make the script above work

As the Humanoid does not have a property called “Power”, the code inside the if statement will never run. Connecting a function to an attribute changing is explained in the tutorial.

so can the script above still work i just need to make a property atrabute

im just wondering because it took a while to write

you will have to rewrite a few things, I’m not going to spoon-feed the code to you though.

i know i want to make it my self im just asking what variables i need to change

What do you mean by what variable? If that’s what I am thinking, when creating an attribute, you can change the value of the attribute.

ok so right now i have it linked to the health attrabute and it works flawlessly i want to make my own Atrabute called Power.

@qut100 already posted the link to the attributes page. If you look at the SetAttribute it will add that to a player, which can be done at the beginning of your script.
Then use your script to change the attribute’s value to whatever you need it to be.

Health of a Humanoid is a Property.
An Attribute is something you add so you can use that for whatever you need.

Would this work with an attribute named Power?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local PowerBar = script.Parent.PowerBar.Bar


local Stamina = 100
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16
local MaxPower = 100
local Power = 1




PowerBar.Size = UDim2.new(1, 0, 1, 0)
Character:WaitForChild("Humanoid").Changed:Connect(function(Attribute)
	if Attribute == "Health" then
		PowerBar.Size = UDim2.new(Character.Humanoid.Health / 100, 0, 1, 0)
		while true do
			if not Running and PowerBar < 100 then
				PowerBar = math.min(PowerBar - 1, 100)
				PowerBar:TweenSize(UDim2.new(PowerBar / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
				
			end
			wait(0.1) 
			print(PowerBar, PowerBar.Size)
			-- Gradually regenerates the Humanoid's Health over time.

			local REGEN_RATE = 1/1000 -- Regenerate this fraction of MaxHealth per second.
			local REGEN_STEP = 1 -- Wait this long between each regeneration step.

			--------------------------------------------------------------------------------

			local Character = script.Parent
			local Humanoid = Character:WaitForChild'Humanoid'

			--------------------------------------------------------------------------------

			while true do
				while Humanoid.Power < Humanoid.MaxPower do
					local dt = wait(REGEN_STEP)
					local dh = dt*REGEN_RATE*Humanoid.MaxPower
					Humanoid.Power = math.min(Humanoid.Power + dh, Humanoid.MaxPower)
				end
				Humanoid.PowerChanged:Wait()
			end
		end
	end
end)

Also because the player is not a part of workspace while editing how can i add the attribute to the player?

would this work?

local HumanoidRootPart = script.Parent

— Listen for one specific attribute change
HumanoidRootPart:GetAttributeChangedSignal(“ReloadTime”):Connect(function()
print(HumanoidRootPart:GetAttribute(“Power”))
end)

– Listen for any attribute change on the instance
HumanoidRootPart.AttributeChanged:Connect(function(Power)
print(Power, HumanoidRootPart:GetAttribute(Power))
end)

Again, don’t call Health an Attribute. It’s already in the Humanoid.

You have to read the documentation. They give an example of how to set attributes.

In your case do something like this at the beginning of your script:

You already reference the local player in your script so you don’t need to do it again.

Character:SetAttribute("power", 3) -- make the 3 whatever you want power to be in the beginning.

then you can listen to see whenever it changes, or just change it directly in your script if you want.

no i made a attribute called power and I changed it from property to health

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local PowerBar = script.Parent.PowerBar.Bar


local Stamina = 100
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16
local MaxPower = 100
local Power = 1




PowerBar.Size = UDim2.new(1, 0, 1, 0)
Character:WaitForChild("Humanoid").Changed:Connect(function(Attribute)
	if Attribute == "Power" then
		PowerBar.Size = UDim2.new(Character.Humanoid.Power / 100, 0, 1, 0)
		while true do
			if not Running and PowerBar < 100 then
				PowerBar = math.min(PowerBar - 1, 100)
				PowerBar:TweenSize(UDim2.new(PowerBar / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
				
			end
			wait(0.1) 
			print(PowerBar, PowerBar.Size)
			-- Gradually regenerates the Humanoid's Power

			local REGEN_RATE = 1/1000 -- Regenerate this fraction of MaxPower per second.
			local REGEN_STEP = 1 -- Wait this long between each regeneration step.

			--------------------------------------------------------------------------------

			local Character = script.Parent
			local Humanoid = Character:WaitForChild'Humanoid'

			--------------------------------------------------------------------------------

			while true do
				while Humanoid.Power < Humanoid.MaxPower do
					local dt = wait(REGEN_STEP)
					local dh = dt*REGEN_RATE*Humanoid.MaxPower
					Humanoid.Power = math.min(Humanoid.Power + dh, Humanoid.MaxPower)
				end
				Humanoid.PowerChanged:Wait()
			end
		end
	end
end)

But in your script you aren’t setting the Attribute.

You have local Power = 1 set at the beginning of the script, but that variable isn’t inside the player, it’s only in the script.

You are waiting to see if something in the Humanoid is changing, but you haven’t put the Attribute in the character, so it’ll never fire the function.

Put Character:SetAttribute("power", 1) instead of your local Power = 1 line.

Ok so these are my 2 scripts would these hypothetical work?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local PowerBar = script.Parent.PowerBar.Bar


local Stamina = 100
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16
local MaxPower = 100
Character:SetAttribute("power", 1)




PowerBar.Size = UDim2.new(1, 0, 1, 0)
Character:WaitForChild("Humanoid").Changed:Connect(function(Attribute)
	if Attribute == "Power" then
		PowerBar.Size = UDim2.new(Character.Humanoid.Power / 100, 0, 1, 0)
		while true do
			if not Running and PowerBar < 100 then
				PowerBar = math.min(PowerBar - 1, 100)
				PowerBar:TweenSize(UDim2.new(PowerBar / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
				
			end
			wait(0.1) 
			print(PowerBar, PowerBar.Size)
			-- Gradually regenerates the Humanoid's Health over time.

			local REGEN_RATE = 1/1000 -- Regenerate this fraction of MaxHealth per second.
			local REGEN_STEP = 1 -- Wait this long between each regeneration step.

			--------------------------------------------------------------------------------

			local Character = script.Parent
			local Humanoid = Character:WaitForChild'Humanoid'

			--------------------------------------------------------------------------------

			while true do
				while Humanoid.Power < Humanoid.MaxPower do
					local dt = wait(REGEN_STEP)
					local dh = dt*REGEN_RATE*Humanoid.MaxPower
					Humanoid.Power = math.min(Humanoid.Power + dh, Humanoid.MaxPower)
				end
				Humanoid.PowerChanged:Wait()
			end
		end
	end
end)

local HumanoidRootPart = script.Parent

— Listen for one specific attribute change
HumanoidRootPart:GetAttributeChangedSignal(“ReloadTime”):Connect(function()
print(HumanoidRootPart:GetAttribute(“Power”))
end)

– Listen for any attribute change on the instance
HumanoidRootPart.AttributeChanged:Connect(function(Power)
print(Power, HumanoidRootPart:GetAttribute(Power))
end)

Screenshot 2023-09-01 131709

would this work better

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local PowerBar = script.Parent.PowerBar.Bar

Character:SetAttribute("power", 1)

PowerBar.Size = UDim2.new(1, 0, 1, 0)

Character:WaitForChild("Humanoid").Changed:Connect(function(Attribute)
    if Attribute == "Power" then
        while true do
            if Character.Humanoid.Power < 100 then
                Character.Humanoid.Power = math.min(Character.Humanoid.Power + 1, 100)
                PowerBar.Size = UDim2.new(Character.Humanoid.Power / 100, 0, 1, 0)
                PowerBar:TweenSize(UDim2.new(Character.Humanoid.Power / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true)
            end
            wait(0.1)
        end
    end
end)