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)
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.
@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.
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)
— 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)
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)
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)
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)