Heyo I’m working on a game that has a stun bar and a health bar however I have no idea how to properly manage/lkeep track of when they change it’s just an absolute mess and I’ve been trying to a find nice clean method to handle it.
In my game I have a script that has a function for preparing the match which spawns the stage and adds certain values into the characters
SetupPlayer(MatchSettings.Player1.Character)
SetupPlayer(MatchSettings.Player2)
Main.Remotes.PrepareMatch:FireAllClients(MatchSettings)
function SetupPlayer(Character)
local Stun = Instance.new("IntValue")
Stun.Name = "Stun"
Stun.Parent = Character
local Player = Main.Players:GetPlayerFromCharacter(Character)
if Player then
for _,Script in pairs(PlayerScripts:GetChildren()) do
Script.Parent = Character
Script.Disabled = false
end
end
Stun:GetPropertyChangedSignal("Value"):Connect(function()
if Stun.Value >= 100 then
warn("STUNNED")
local IsStunned = Instance.new("BoolValue")
IsStunned.Name = "Stunned"
IsStunned.Parent = Stun.Parent
Main.Remotes.Stunned:FireAllClients()
end
end)
end
So I use GetPropertyChangedSignal here when I make the value this probably isn’t orgranized, but I don’t know where to put it so yeah so now I have this on the client this is how I update the gui.
local function StunChanged(Character)
Character.Stun:GetPropertyChangedSignal("Value"):Connect(function()
--Increasing Bar
local NewStun = Character.Stun.Value
local StunBar = Main.ScreenGui.Match[Character.Name].StunBarBackground.StunBar
local Stun = math.clamp((NewStun/100),0,1)
Modules.TweenManager.MakeTween(0,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,StunBar,{Size = UDim2.fromScale(Stun,StunBar.Size.Y.Scale)},false,true)
--For Decreasing Bar doesn't work :[
--The goal here was to make it so if you don't get hit within 2 seconds decrease the stun
coroutine.wrap(function()
while true do
if Cooldowns[Character.Name] and os.clock() - Cooldowns[Character.Name] > 2 then
Modules.TweenManager.MakeTween(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,StunBar,{Size = UDim2.fromScale(Stun,StunBar.Size.Y.Scale)},false,true)
break
end
wait()
end
end)()
Cooldowns[Character.Name] = os.clock()
end)
end
Really sorry for the cringe coding, but I honestly have no idea what I’m doing so yeah looking for ways to improve this.