Is there a less laggy way to go about this?

local Player = game.Players.LocalPlayer
local Character = Player.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")

while true do
	local X = math.floor(RootPart.Position.X)
	local Y = math.floor(RootPart.Position.Y)
	local Z = math.floor(RootPart.Position.Z)
	script.Parent.Text = X.." "..Y.." "..Z
	wait()
end

This is a coordinates display, but having it be a while loop is considerably laggy for the game. is there another, less laggy way of approaching this?

1 Like

Maybe instead of the whole loop you could change it to this (so it’s not constantly firing):

Humanoid.Running:Connect(function()
-- Your code here
end)

How could I also account for jumping, swimming, and falling?

Check the Humanoid’s state type like this:

local State = Humanoid:GetState()

if State == Enum.HumanoidStateType.Jumping then
-- Your code would go here
end

You could also do this:

Humanoid.StateChanged:Connect(function(oldstate, newstate)
if newstate == Enum.HumanoidStateType.Jumping then
-- Your code would go here
end
end)

Wouldn’t this just update it the coords when a jump/swim/walk/fall/run is initiated (when removing the if statement) and not update it while those states are playing out?

I’m not sure what you exactly mean. However, if I understood you correctly, no, it (the event doesn’t change the coordinates) doesn’t update the coordinates of the player’s character.

To rephrase, this would only update the coords as soon as the state change happens (like walking to swimming), but would not continue to update the coords as the player continued to swim. It would only do so if it had another change like jumping out of the water.

Like after the StateChanged event fires you can spawn a coroutine inside that consistently updates. Then when the state changes again you can then yield the coroutine.

This would reduce the amount of looping. But should work the same, I don’t know about decreasing the lag.

local plr = game.Players.LocalPlayer
local chr = plr.Character
local Hum = chr:WaitForChild('Humanoid')

while task.wait() do
	local MoveDirection = math.round(Hum.MoveDirection.Magnitude)
	if MoveDirection == 1 or Hum:GetState() ~= Enum.HumanoidStateType.Running then
		print('changed')
	else return end
end

Actually, it should decrease the lag, as I’m using it for printing which will increase the lag, but if you’re just changing a text, then It should remove the lag.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")

game:GetService("RunService").Heartbeat:Connect(function()
    local X = math.floor(RootPart.Position.X)
    local Y = math.floor(RootPart.Position.Y)
    local Z = math.floor(RootPart.Position.Z)
    script.Parent.Text = X .. " " .. Y .. " " .. Z
end)

The Heartbeat event is less laggy cuz it is tied to the frame rate of the game it fires once per frame, which occurs around 60 times per second

Positive points :

smoother updates and better performance

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.