Question, How Would I Track The Player's Y Position And Put It In LeaderStats

So I Know how to Track the Y Position of normal objects like

--Please Correct me If I'm wrong
local Part1 = game.Workspace:WaitForChild("Part1")
local Part1Position = Part1.Position.Y

I just Don’t know how to Put It On LeaderStats
So Please, All Answers Help, Thank you.

I don’t know how much you know about leaderstats but basically:

  1. Insert a folder named ‘leaderstats’ into the player
  2. Add a “value object” i.e. NumberValue, StringValue (in this case number) into the leaderstats folder
  3. Rename the value object to what you want it to show up as in the leaderstats (something like ‘Height’)
  4. Change the value of that object to whatever the value should show on the leaderstats (in this case the part’s y position)

Example code:

game:GetService('Players').PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats'
	
	local value = Instance.new('NumberValue', leaderstats)
	value.Name = 'Example value'
	value.Value = 5
end)

Follow @Orbular3 's Example, it is the correct and common way to create leaderstats. Plus, those Variables are used in this Example, pretty simple one.


Might Work However,
Script:

-- Above Should be PlayerAdded stuff seen in Orbular3's Example
player.CharacterAdded:Connext(char)
while task.wait() do

value.Value = math.floor(char.PrimaryPart.Position.Y)
  end
end)
end) -- End of Orbular3's PlayerAdded example

It might be preferable, if time isn’t an issue for OP, to use an event-based paradigm here as opposed to a continuously running loop that uses more resources.

With that in mind, here is how I developed a co-ordinate system that only updates via events.

Could be helpful in refactoring your code.

the while loop shouldnt interupt the script before it, it isnt exactly a waste a rescources compared to other examples

It’s better to update the value only when the value is changed compared to every frame as then the code will only be run when necessary - for example, you don’t want the loop to continue when the player is just on the floor because that’s a waste

Just better optimisation overall to use events instead

1 Like

Hold On, Where Do I Put The script and what Type of script?

You’re right, it isn’t.

If this practice were to be implemented in a game with 50 players a server however, with extra code executing as the player’s position changes, then the server has 50 threads running simultaneously all accounting for the same variables spanning across 50 players.

With event-based programming, only one loop has to be active at a time in the background, allowing the developer to abstract away logic and focus on imperatives influencing the values and attributes of the player.

Hope this clarifies why it doesn’t matter at this stage, but would in a context with more players.

If you’re looking to execute changes to the player globally, then use a server script in ServerScriptService.

If you’re looking to execute changes to the player locally (on their client), then use a local script in StarterPlayerScripts.

Use a Server Script, place it into ServerScriptService, thats if you want it visible to everyone.

What Is a “Server Script”? :neutral_face: :neutral_face:

ServerScript is just a regular Script

I refer to it as a ServerScript to be more Clear for certain purposes, and the fact it runs on the Server

ServerScript (Script)
ClientScript (LocalScript)
ModuleScript (Normal Name of Instance)

Something is not working, How do I identify the HumanoidRootPart (e.g.

local HumanoidRootPart = --Stuff like that

I usually just use PrimaryPart instead of the HumanoidRootPart since its pretty much the same thing

But @DasKairo how do I Identify it? :thinking: :thinking:

You Indentify it if its inside of a model, but in this case:
(If you are using the examples)

player.Character.PrimaryPart
-- or this:
Char.PrimaryPart

Does anyone have the code to Identify the HumanoidRootPart? @xGOA7x
@Orbular3 @anon_j1124

Bruh

game:GetService('Players').PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats'
	
	local value = Instance.new('NumberValue', leaderstats)
	value.Name = 'Example value'
	value.Value = 5

player.CharacterAdded:Connect(char)
while task.wait() do

value.Value = math.floor(char.PrimaryPart.Position.Y)
  end
end)
end)

Thats literally all im telling you to do.

Sorry, @DasKairo PrimaryPart Doesn’t seem to work.

Fixed my Error: Should work now

game:GetService('Players').PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats'
	
	local value = Instance.new('NumberValue', leaderstats)
	value.Name = 'Example value'
	value.Value = 5

player.CharacterAdded:Connect(function(char)
while task.wait() do

value.Value = math.floor(char.PrimaryPart.Position.Y)
  end
end)
end)