Hi!
I’ve been setting up a script to keep track of players’ altitudes, relative to a part, and it seems to work pretty well! Although, I’ve been having a slight issue that I just can’t seem to figure out.
For whatever reason, the script only seems to run twice, despite being a while true do
loop. I can’t for the life of me figure out what might be causing this, so I’m hoping someone here can help. I’ve included the script down below:
Altitude tracker script
-- function to round numbers, duh
local function round(n)
return math.floor(n + 0.5)
end
-- track player's altitude
local altitudeRef = Workspace.testRef
while true do
for i,v in pairs(game.Players:GetChildren()) do
local leaderstats = v.leaderstats
local maxHeight = leaderstats and leaderstats:FindFirstChild('Highest Climbed')
local altitude = leaderstats and leaderstats:FindFirstChild('Altitude')
print(tostring(round(altitudeRef.Position.Y)))
v.CharacterAdded:Wait()
print("Character added.")
if v.Character then
if altitude then
if not v.Character.UpperTorso then
charPos = v.Character.Torso.Position.Y
else
charPos = v.Character.UpperTorso.Position.Y
end
altitude.Value = (round(charPos - altitudeRef.Position.Y))
end
if maxHeight then
if maxHeight.Value < altitude.Value then maxHeight.Value = altitude.Value end
end
end
end
wait(0.1)
end
What’s the use for the v.CharacterAdded:Wait()?
That means that it’s trying to wait for the character to be added and then reference it.
Try replacing that line with
local character = v.Character or v.CharacterAdded:Wait()
So that it will grab the character if it’s there, or if it needs to be made, then change your v.Character
references to the character variable.
2 Likes
Ah, you’re right! I should’ve realised, it’s waiting for my character to be added EVERY time it runs. Thanks for helping me clear this up.
2 Likes
Of course!
Now I just need one more solution. 
1 Like
Also, the way I would do that is connect to the Heartbeat event of RunService rather than using a while true do loop.
https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat

1 Like
Ah, alright! I wasn’t aware that was a thing.
Although, now I’ve noticed, it stops tracking the character’s altitude when they die and respawn. Hmm…
1 Like
I just tested it. Still works just fine for me after a respawn… Here’s the full code I’m using to test.
--// Name: Script.lua
--// Author: Danny (MrLonely1221)
--// Description:
--// Variables
--// Functions
local function round(n)
return math.floor(n + 0.5)
end
--// Events
game:GetService("Players").PlayerAdded:Connect(function(player)
local stats = script.leaderstats:Clone();
stats.Parent = player;
end);
game:GetService("RunService").Heartbeat:Connect(function()
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
local leaderstats = v.leaderstats
local maxHeight = leaderstats and leaderstats:FindFirstChild('Highest Climbed')
local altitude = leaderstats and leaderstats:FindFirstChild('Altitude')
local character = v.Character or v.CharacterAdded:Wait()
local charPos
if character then
if altitude then
if not character.UpperTorso then
charPos = character.Torso.Position.Y
else
charPos = character.UpperTorso.Position.Y
end
altitude.Value = round(charPos)
end
if maxHeight then
if maxHeight.Value < altitude.Value then maxHeight.Value = altitude.Value end
end
end
end
end)
1 Like
Yeah, I think since it’s a while true
loop it’s crashing the script when the player falls off the map and there’s suddenly no Character anymore, so I should try writing an exception handler for that, ooor… seeing if Hearbeat fixes it.
1 Like
If you’re jumping off the map it’s suddenly changing the parent of the character to nil so the problem with that is it may try and reference something there (being the UpperTorso), and then it breaks there.
If they reset or their health drops to 0, it will continue to run, if the character is deleted and it tosses a “nil value” error in the script it will break it.
1 Like
Yeah, I guess I’ll try to figure out how to write an exception handler for if they fall off the map.
You could just toss the whole thing in a pcall inside of the for loop for each player.
1 Like
That… caused my Roblox Studio to almost crash. I might be doing something wrong.
1 Like
What does your code look like?
1 Like
Ah, nevermind. Switching from while true
to heartbeat fixed it, using the pcall. When I made that post, I was just using pcall, and it was still a while true loop. Thanks for your help!
1 Like