This script basically handles what happens when a player joins my game. (Like creating the stats such as hunger, thirst, and health.) It works perfectly sometimes, but other times The gui stats bars don't decrease but the script will still kill the player when the stat reaches 0. Any Idea what the problem could be?
wait(5 + math.random()) local SUMMER_CELEBRATION = 1 local PLAYER = script.Parent.Parent local CHAR = PLAYER.Character local ALIVE = true function modStat(stat, val) local final_value = stat.Value + val if final_value > 100 then final_value = 100 elseif final_value < 0 then final_value = 0 end stat.Value = final_value end function killPlayer() local humanoid = CHAR.Humanoid if humanoid ~= nil then humanoid.Health = 0 end ALIVE = false end function addDoonceCheck() local doonce = Instance.new("IntValue") doonce.Name = "DoOnce" doonce.Parent = PLAYER end function addRef(parent, ref, name) local reference = Instance.new("ObjectValue") reference.Value = ref reference.Name = name reference.Parent = parent end function do_make_survivor() local c = CHAR:GetChildren() for index, child in pairs(c) do if child.className == "Hat" then child:remove() elseif child.className == "Pants" then child:remove() elseif child.className == "Shirt" then child:remove() end end local c2 = CHAR.Torso:GetChildren() for index, child in pairs(c2) do if child.className == "Decal" then child:remove() end end local bc = CHAR["Body Colors"] bc.HeadColor = BrickColor.new("Cool yellow") bc.TorsoColor = BrickColor.new("Cool yellow") bc.RightArmColor = BrickColor.new("Cool yellow") bc.LeftArmColor = BrickColor.new("Cool yellow") bc.RightLegColor = BrickColor.new("Brown") bc.LeftLegColor = BrickColor.new("Brown") end function do_connect_ondeath() local human = CHAR.Humanoid human.Died:connect(on_death) end function on_death() --Remove backpack items to worldspace, remove stored tools to nil ALIVE = false --Stop da loop! script.Parent = PLAYER wait(1) local pos = CHAR.Head.Position local pack = PLAYER.Pack:GetChildren() local tools = PLAYER.Tools:GetChildren() for index, child in pairs(tools) do child:remove() end for index, child in pairs(pack) do child.Position = pos child.Parent = game.Workspace child.Locked = false wait() end script:remove() end function onEnter() local doonce_check = PLAYER:findFirstChild("DoOnce") if doonce_check == nil then addDoonceCheck() local pack = Instance.new("Model") pack.Name = "Pack" pack.Parent = PLAYER local packmax = Instance.new("NumberValue") packmax.Name = "PMax" packmax.Parent = PLAYER packmax.Value = 24 local bldgs = Instance.new("Model") bldgs.Name = "Buildings" bldgs.Parent = PLAYER local Animals = Instance.new("Model") Animals.Name = "Animals" Animals.Parent = PLAYER local tools = Instance.new("Model") tools.Name = "Tools" tools.Parent = PLAYER local stats = Instance.new("Model") stats.Name = "Stats" local he = Instance.new("NumberValue") he.Name = "Health" he.Value = 100 he.Parent = stats local hu = Instance.new("NumberValue") hu.Name = "Hunger" hu.Value = 100 hu.Parent = stats local th = Instance.new("NumberValue") th.Name = "Thirst" th.Value = 100 th.Parent = stats stats.Parent = PLAYER end end function getDead(he, hu, th) local bool = false bool = (bool or he.Value <= 0) bool = (bool or hu.Value <= 0) bool = (bool or th.Value <= 0) return bool end function getDrowning() local torso = CHAR.Torso return (torso.Position.y < 19 and torso.Position.y > 0) end function getStats() local stats = PLAYER.Stats local he = stats.Health local hu = stats.Hunger local th = stats.Thirst return he, hu, th end onEnter() do_make_survivor() do_connect_ondeath() local he, hu, th = getStats() while ALIVE do modStat(hu, -0.005 * SUMMER_CELEBRATION) modStat(th, -0.01 * SUMMER_CELEBRATION) if getDrowning() then modStat(he, -0.5) else modStat(he, 0.01 * SUMMER_CELEBRATION) end if getDead(he, hu, th) then modStat(he, 100) modStat(hu, 100) modStat(th, 100) killPlayer() end wait(0.1) end print("DEAD")