local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
while true do
wait()
script.Parent.Text = char.Values.Oxygen.Value
end
for some reason whenever the character dies this script stops working and doesn’t display oxygen, please help
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
local oxygenLabel = script.Parent
local function checkOxygen()
local char = localPlayer.Character --Don't need to say "or characteradded:wait()" because the function returns and the loop will just check it again!
if not char then return end
local values = char:FindFirstChild("Values")
if not values then return end
local oxygenValue = values:FindFirstChild("Oxygen")
if not oxygenValue and not oxygenValue:IsA("ValueBase") then return end
oxygenLabel.Text = oxygenValue.Value
end
while task.wait() do
checkOxygen()
end
Tips:
1st: Don’t store important things under or as an attribute to the player’s character model, since their client changes in their avatar replicate to the server, meaning that your values under the character can be exploited. Store them in the player, (or in the server storage, but that is more advanced and would require remote functions)
2nd: If you don’t like those many lines of code, (which are there just to ensure that the script won’t break, and won’t slow it down at all) you can use coroutines or pcalls, etc…
3rd: The category is actually scripting support
4th: Forgot about it, but if you are not sure if something exists or not, use findfirstchild instead of the dot operator (don’t use it in player.Character since that’s a property of the player and won’t error if it doesn’t exist).