This script decreases the value by 20 every 2 seconds (temporary numbers) but when it reaches zero it wont make the characters skin grey, why?
local Character = script.Parent.Parent
local CharacterConfig = script.Parent.Parent:WaitForChild("CharacterConfig")
local Blood = CharacterConfig:WaitForChild("Blood")
local MaxBlood = CharacterConfig:WaitForChild("MaxBlood")
local TweenService = game:GetService("TweenService")
while true do
while CharacterConfig.Blood.Value > 0 do
Blood.Value = Blood.Value - 20
wait(2)
end
Blood.Changed:Wait()
end
while true do
if Blood.Value ==0 then
Blood.value = 0
for i,part in pairs(Character:GetDescendants()) do
if part:IsA("Part") or part:IsA("MeshPart") then
TweenService:Create(part,TweenInfo.new(4),{Color = Color3.fromRGB(187, 179, 178)}):Play()
end
end
end
Blood.Changed:Wait()
end
It’s because the first while loop is being reached, but because the condition for that while loop to run is:
while true do
True is well, always true, which is why that while will keep on looping, forever and ever and ever. This is fine if you want a continuous loop, but this means your second one below will never be reached, because it’s being stuck on the first.
The best way around this is as @FloofyPlasma mentioned, you should be putting the logic from your second while loop into the first, that way the code can run.
local Character = script.Parent.Parent
local CharacterConfig = script.Parent.Parent:WaitForChild("CharacterConfig")
local Blood = CharacterConfig:WaitForChild("Blood")
local MaxBlood = CharacterConfig:WaitForChild("MaxBlood")
local TweenService = game:GetService("TweenService")
while true do
while CharacterConfig.Blood.Value > 0 do
Blood.Value = Blood.Value - 20
if Blood.Value == 0 then
for i,part in pairs(Character:GetDescendants()) do
if part:IsA("Part") or part:IsA("MeshPart") then
TweenService:Create(part,TweenInfo.new(4),{Color = Color3.fromRGB(187, 179, 178)}):Play()
end
end
break
end
wait(2)
end
Blood.Changed:Wait()
end
We need to break out of the inner while loop so that the check above to see whether the blood value > 0 can be checked again. I’ve also removed a set of Blood.Value as it was being set in the check to see whether it was equal to 0 (so if it was equal to 0, why set it to 0?).
That set in question was in the second while loop:
while true do
if Blood.Value == 0 then
Blood.Value = 0 --[[ Here. It's performed in the check that checks for
it's value to be 0. So this will only run if the Blood.Value is 0 anyway. ]]--
for i,part in pairs(Character:GetDescendants()) do
if part:IsA("Part") or part:IsA("MeshPart") then
TweenService:Create(part,TweenInfo.new(4),{Color = Color3.fromRGB(187, 179, 178)}):Play()
end
end
end
Blood.Changed:Wait()
end
local function OnBloodChanged(NewBlood)
if NewBlood ~= 0 then return end
for _, Descendant in ipairs(Character:GetDescendants()) do
if Descendant:IsA("BasePart") then
TweenService:Create(Descendant, TweenInfo.new(4), {Color = Color3.fromRGB(187, 179, 178)}):Play()
end
end
end
Blood.Changed:Connect(OnBloodChanged)