hello im trying to make a knocked system where if a player is below or equal to a certain health they will be knocked out (down). but for some reason my script is bugging out. the knocked part works but then some times the regain health function stops working or it will break (not work) when the regain health function is running and the player takes damage. Could the reason be that i already have another local script that operates another separate function when the player looses health?
– local script in starterPlayerScripts for Knocked System
-- Define events and player
local carryEvent = game.ReplicatedStorage:WaitForChild("Carry")
local gripEvent = game.ReplicatedStorage:WaitForChild("Grip")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
-- Wait for the character to load
repeat wait() until char
print("char loaded")
-- Get the Humanoid object
local hum = char:WaitForChild("Humanoid")
-- Set initial attributes
char:SetAttribute("Knocked", false)
char:SetAttribute("IsKnocked", false)
char:SetAttribute("CanCarry", true)
-- Define health-related variables
local knockedHealth = 5
local getBackUpHealth = 10
local healthTime = 1
local healBy = 1
-- Function to regain health over time
local function regainHealth()
while hum.Health <= knockedHealth and char:GetAttribute("Knocked") do
hum.Health = hum.Health + healBy
task.wait(healthTime)
if hum.Health >= getBackUpHealth then
char:SetAttribute("Knocked", false)
print("player is back on thier feet")
break
end
end
end
-- Function to handle health changes
local function healthChanged()
if hum.Health <= knockedHealth then
char:SetAttribute("Knocked", true)
print("Player is down")
regainHealth()
end
end
hum:GetPropertyChangedSignal("Health"):Connect(healthChanged)
-- Connect the function to the Health changed signal
hum:GetPropertyChangedSignal("Health"):Connect(healthChanged)
– local script in starterGui for healthBar
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function(Damage)
local bar = script.Parent.BackFrame.bar
bar.Size = UDim2.new(Damage / humanoid.MaxHealth, 0, 1, 0)
end)