I want to make a script that shows the player their position, but it isn’t working while the players is moving and only updates when they stop. Any solutions?
local player = game.Players.LocalPlayer
local textLabel = script.Parent
local chara = player.Character
if not chara then
chara = player.CharacterAdded:Wait()
end
local hum = chara.Humanoid
hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
local function convertPos()
local pos = chara.HumanoidRootPart.Position
local xPos = math.round(pos.X)
local yPos = math.round(pos.Y)
local zPos = math.round(pos.Z)
textLabel.Text = tostring(xPos) .. tostring(yPos) .. tostring(zPos)
end
convertPos()
end)
Yeah I was starting to write about that haha. If I may give one suggestion though, it would be a good idea to use string formatting for the numbers, just because sometimes issues can arise just concatenating numbers.
I think instead of using the MoveDirection, it may be better to use the Running event listener.
humanoid.Running:Connect(function(speed)
if speed > 0.04 then
local pos = chara.HumanoidRootPart.Position
local xPos = math.round(pos.X)
local yPos = math.round(pos.Y)
local zPos = math.round(pos.Z)
textLabel.Text = tostring(xPos) .. tostring(yPos) .. tostring(zPos)
end
end)
nevermind, I just figured out that if you put the block of code in a while loop it works! =D
local player = game.Players.LocalPlayer
local textLabel = script.Parent
local chara = player.Character
if not chara then
chara = player.CharacterAdded:Wait()
end
local hum = chara.Humanoid
while hum:GetPropertyChangedSignal("MoveDirection") do
local function convertPos()
local pos = chara.HumanoidRootPart.Position
local xPos = math.round(pos.X)
local yPos = math.round(pos.Y)
local zPos = math.round(pos.Z)
textLabel.Text = tostring(xPos) .. ", " .. tostring(yPos) .. ", " .. tostring(zPos)
end
convertPos()
task.wait()
end
Yeah because it’s relying on hum:GetPropertyChangedSignal being a truthy value, and is constantly updating the text even if the player’s not moving. It takes up more compute time than what’s necessary.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid
local hrp = char.HumanoidRootPart
local isOnTheMove = false
local nonRunningConditions = {
[Enum.HumanoidStateType.Landed] = true,
[Enum.HumanoidStateType.PlatformStanding] = true,
[Enum.HumanoidStateType.Dead] = true,
[Enum.HumanoidStateType.None] = true
}
hum.StatedChanged:Connect(function(_, currentState)
if not nonRunningConditions[currentState] and not isOnTheMove then
isOnTheMove = true
while isOnTheMove do
local pos = hrp.Position
local xPos = math.round(pos.X)
local yPos = math.round(pos.Y)
local zPos = math.round(pos.Z)
textLabel.Text = string.format("%d:%d:%d", xPos, yPos, zPos)
task.wait(1/33.333)
end
else
isOnTheMove = false
end
end)
What this’ll do is listen for when the humanoid’s state changes, sees if they’re running and updates the text until the condition is no longer running. It might require some tinkering though.
Thank you for the help! I credited you in the code for your work! and I fixed a couple typos! =D
local textLabel = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid
local hrp = char.HumanoidRootPart
local isOnTheMove = false
hum.StateChanged:Connect(function(_, currentState)
if currentState == Enum.HumanoidStateType.Running then
isOnTheMove = true
while isOnTheMove do
local pos = char.HumanoidRootPart.Position
local xPos = math.round(pos.X)
local yPos = math.round(pos.Y)
local zPos = math.round(pos.Z)
textLabel.Text = string.format("%d, %d, %d", xPos, yPos, zPos)
task.wait(1/33.333)
end
elseif currentState == Enum.HumanoidStateType.None then
isOnTheMove = false
end
end)
--Credits to TheeDeathCaster on the devforum for this code!
Awesome sauce. It’ll probably still need some tinkering with a new edit, but it should get the job done. Hopefully the wait interval isn’t too long either. =p