Horizontal position bar not working properly

  1. What do you want to achieve? Keep it simple and clear!
    A position bar that shows the player’s X position on it

  2. What is the issue? Include screenshots / videos if possible!
    I can’t seem to get it working completely right. My character is always off the position bar, and when I did once get it working, when I changed the tunnel length it broke.
    Here is a link to a video:

  1. What solutions have you tried so far?
    I have looked on the developer hub, and there are a few similar problems, but any that I have tried haven’t worked.

Here is my code:

local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local TunnelLength = tonumber(replicatedStorage.Values.TunnelLength.Value)

runService.Heartbeat:Connect(function()
	local character = workspace:FindFirstChild(script.Parent.Name)
	if not character then return end
	local hrp = character:FindFirstChild("HumanoidRootPart")
	local leftLeg = character:FindFirstChild("Left Leg")
	if not hrp or not leftLeg then
		warn("Not found")
		return
	end
	script.Parent.Position = UDim2.new(tonumber(-hrp.Position.X/TunnelLength), 0, 0, 0)
end)

replicatedStorage.Values.TunnelLength.Changed:Connect(function(value)
	TunnelLength = tonumber(value)
end)

Any help is greatly appreciated!

Edit: Here is a picture of my explorer (The disabled local script is the script that isn’t working)

explorer

-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Variables
local Player = Players.LocalPlayer

-- Run Service
RunService.Heartbeat:Connect(function()
   local Character = Player.Character or Player.CharacterAdded:Wait()
   local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
   local TunnelLength = ReplicatedStorage:WaitForChild("Values"):WaitForChild("TunnelLength").Value

   script.Parent.Position = UDim2.fromScale(HumanoidRootPart/ TunnelLength,0) 
end) 

I think this may work, I have shorten the code as well, although the way you have is totally fine. I have one suggestion, you could have a attribute set in workspace that has the current length, so that way you don’t need an event to change the TunnelLength you can just wait for it to get changed. (on the server). Hope this helps.

1 Like

Thank you, I will go into studio to try this out soon!

Now I appear to be going backwards lol:

I would try something like this.

Depth = math.clamp((TunnelStart.Position - playerPosition).Magnitude, 0, tunnelLength)/tunnelLength

1 Like

How would I incorporate this into my main script?

Depth is how deep you are in that course.

So Marker.Position = UDim2.new(Depth, 0, 0, 0),

So first you mark out where your course starts. That’s gonna be TunnelStart.Position and then to find how far away you are, you would do (Start - Root.Position).Magnitude.(I am doing this so even if course is rotated and not aligned perfectly with any axis it would still work) and then divide it by tunnels length and then make it so value can’t exceed 1 just in case.

Ah ok, thanks, I am going into studio to try this out now!

Wow, it works! The only problem is that when I jump, it also moves me forwards on the bar - is there any way to fix this?

You would do ((start - playerPos) * Vector3.new(1,0,1)).Magnitude

Wow thank you dude!

Sorry to bother again, but there is a small problem where it isn’t completely accurate:

As you can see, when I reach an intersection, the position bar says that I am further from it.

These are the lines of code that you gave me and I have put in:


local depth = math.clamp((game.Workspace.Tunnel.Lobby.Main.Shell.Start.Position - hrp.Position * Vector3.new(1,0,1)).Magnitude, 0, TunnelLength)/TunnelLength
script.Parent.Position = UDim2.new(depth, 0, 0, 0)

What my code is doing is simplest way to show progeress. Basically from start to end how much have you proceeded in %s(0-1) if you want it to be more accurate you would have to make script also display intersections itself as human won’t be as accurate. Or you will have to add points at intersections and use precentages at every chunk yourself.

Oh ok, thank you very much for the help!