Hello I just tried this script to make a height bar but it seems to not work
Anyone can help me fix the issue? Thanks
They are both LocalScripts in ScreenGUI > Frame > ImageLabel
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:FindFirstChild("PlayerGui")
if Player:FindFirstChild("PlayerGui") then
while wait() do
local ImageLabel = PlayerGui.HeightGui.Frame:FindFirstChild("ImageLabel")
local Character = game:GetService("Workspace")[Player.Name]
local PrimaryPart = Character:FindFirstChild("UpperTorso") -- Torso for R6, UpperTorso for R15
local Height = math.ceil(PrimaryPart.Position.Y)
ImageLabel.Position = UDim2.new(0, 0,0 - Height / 50, 0) -- Change 50 to another to adjust height
end
end
And
local ThumbnailType = Enum.ThumbnailType.HeadShot
local ThumbnailSize = Enum.ThumbnailSize.Size48x48
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
while wait() do
script.Parent.Image = Players:GetUserThumbnailAsync(Player.userId, ThumbnailType, ThumbnailSize)
end
Also I think you should rename “PrimaryPart” to something else, and also find HumanoidRootPart, not the upper torso(that’s because the bar will shake a bit, cuz its moving when you are walking)
while true do
wait()
local PlayerCharacter = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
if PlayerCharacter ~= nil then
script.Parent.Frame.Visible = true
local Pos = ((PlayerCharacter["HumanoidRootPart"].Position.Y - 2.825)/50) * script.Parent.Frame.AbsoluteSize.X
script.Parent.Frame.ImageLabel.Position = UDim2.new(0,Pos,0,0)
else
script.Parent.Frame.Visible = false
end
end
There’s no need to add a space before the equal sign, if you want to do it for “making the code look clean”, you can do it; but if you dont want to, you dont need to.
Try also to avoid to use wait(), since wait() has a variable time. On slower servers/devices wait() can have a longer duration than expected. I’d recommend if you’re on the client, that you wait for the renderstepped/heartbeat/stepped event. Also, I’d also recomend instead of getting imagelabel, character and primary part each time you run the loop, you get those before the while, since you dont need to calculate those each time. The same applies to
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:FindFirstChild("PlayerGui")
if Player:FindFirstChild("PlayerGui") then
while wait() do
local ImageLabel = script.Parent.Frame:FindFirstChild("ImageLabel")
local Character = game:GetService("Workspace")[Player.Name]
local PrimaryPart = Character:FindFirstChild("UpperTorso") -- Torso for R6, UpperTorso for R15
local Height = math.ceil(PrimaryPart.Position.Y)
ImageLabel.Position = UDim2.new(0, 0,(0 - Height / 50), 0) -- Change 50 to another to adjust height
end
end
As you can see, I don’t used PlayerGui, just script.Parent
Well, in this case it wouldnt do alot doing it one way or the other. But when you only need to get things 1 time, you dont need to calculate them all the time. Also sometimes if you’re using some roblox api calls, if you request too much of them too quickly, you may pass the rate and have that return nil. So its always good to “calculate”/get things the number of times you need them. And as for wait(), its good to try to avoid it at all cost.