I’ve attempted to create a really basic health bar using Roact. Typically, I’d create my own component system for UIs; however, I thought I’d give Roact a try, and see how I got on. I’d just like some feedback to see if I’ve gone about this right.
To be honest, a Roact tutorial for creating basic Roblox UI components (e.g. health bar, money display, leaderboard, player list, etc.) would be greatly appreciated. I’d love to know how to utilise Roact properly. It seems like a great system to use, I just struggle a little bit to understand it fully.
Here's the source code for my little health bar:
local Replicated, Players = game:GetService 'ReplicatedStorage', game:GetService 'Players'
local Roact, Player = require(Replicated.Roact), Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HealthBar = Roact.PureComponent:extend 'HealthBar'
function HealthBar:init()
local Humanoid = Player.Character:WaitForChild 'Humanoid'
self.state = {
health = Humanoid.Health,
maxhealth = Humanoid.MaxHealth
}
local function UpdateHealthState()
self:setState({
health = Humanoid.Health,
maxhealth = Humanoid.MaxHealth
})
end
local _HumanoidChangedSignal = Humanoid.Changed:Connect(UpdateHealthState)
Player:GetPropertyChangedSignal 'Character':Connect(function()
_HumanoidChangedSignal:Disconnect()
Humanoid = Player.Character:WaitForChild 'Humanoid'
_HumanoidChangedSignal = Humanoid.Changed:Connect(UpdateHealthState)
end)
end
function HealthBar:render()
return Roact.createElement('ImageLabel', {
BackgroundTransparency = 1,
Position = UDim2.new(0, 16, 0, 16),
Size = UDim2.new(0, 300, 0, 18),
ClipsDescendants = true,
Image = 'rbxasset://textures/whiteCircle.png',
ImageColor3 = Color3.fromRGB(225, 225, 225),
ScaleType = Enum.ScaleType.Slice,
SliceCenter = Rect.new(256, 256, 256, 256)
}, {
Amount = Roact.createElement('ImageLabel', {
BackgroundTransparency = 1,
Size = UDim2.new((self.state.health / self.state.maxhealth), 0, 1, 0),
Image = 'rbxasset://textures/whiteCircle.png',
ImageColor3 = Color3.fromRGB(48, 161, 51),
ScaleType = Enum.ScaleType.Slice,
SliceCenter = Rect.new(256, 256, 256, 256)
})
})
end
function RenderApp()
Roact.mount(Roact.createElement(HealthBar), script.Parent, 'HealthBar')
end
RenderApp()
RoactHealthBar.rbxm (1,8 KB)