What do you want to achieve? Keep it simple and clear!
A position bar that shows the player’s X position on it
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:
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)
-- 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.
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.
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.