[UNSOLVED] Inaccurate height bar results

Hello there, I am making a game similar to Tower of Hell and I came across to an issue. My height bar is inaccurate.

Script:

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

local towerHeight = tonumber(replicatedStorage.TowerHeight.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 return end
	script.Parent.Position = UDim2.new(0, 0, 1 - ((hrp.Position.Y - (hrp.Size.Y/2 + leftLeg.Size.Y))/towerHeight), 0)
end)

replicatedStorage.TowerHeight.Changed:Connect(function(value)
	towerHeight = tonumber(value)
end)

Also here’s a screenshot of the height bar.

The calculation just needs to be a ratio restricted on [0, 1] between the vertical position of the player’s torso and the tallest point of the tower.

Your calculation should be something like this:

local tallestPoint = ...
local percentage = math.clamp(player.HumanoidRootPart.Position.Y / tallestPoint, 0, 1)
-- use percentage to place point

Think of the position of the height arrow as the top point of an invisible bar contained in the height display. Placing the arrow at the top of this invisible bar and scaling the bar using the percentage calculated should give the proper vertical player position.

game:GetService("RunService").Heartbeat:Connect(function()
    invisibleBar.Size = UDim2.fromScale(0, percentage) -- equivalent to UDim2.new(0, 0, percentage, 0)
end)

Sorry to ask you this but, how do I get the tallest point? I always get a value of zero.

the tallestPoint is supposedly the point where the end of the tower is on the Y axis.

local tallestPoint = workspace.TallestPointPart.Position.Y -- example
1 Like

It still gives me a response of zero.

Here’s the code, I don’t understand what’s wrong:

local runService = game:GetService("RunService")
local char = game.Players.LocalPlayer.Character

repeat wait() until game.Players.LocalPlayer.CharacterAdded
wait(1)

local tallestPoint = workspace.TallestPointPart.Position.Y
local percentage = math.clamp(char.HumanoidRootPart.Position.Y / tallestPoint, 0, 1)

print(percentage)

runService.Heartbeat:Connect(function()
	script.Parent.Parent.Size = UDim2.fromScale(0, percentage)
end)

(and yes, I have a TallestPointPart in the workspace.)

Debug your code. See if tallestPoint is actually taking on the proper value. If it’s not, perhaps your issue lies in replication somewhere and the client simply cannot see the true Y-component of the position of the tallest point.

FYI: use task.wait() instead of wait.

Also use game.Players.LocalPlayer.CharacterAdded:Wait() to listen for the character spawning. CharacterAdded is an event, not a property, so the line

repeat wait() until game.Players.LocalPlayer.CharacterAdded

doesn’t make a whole lot of sense (when is an event true?)

1 Like