Hi, I’m making a glass bridge game, and I want other players to locate other players via a player length bar. If you don’t know what I mean by that, look at the following image.
The thing is, I don’t know how to make this at all. If anyone could walk me through how to make one, because everything on YouTube is not helpful, there are no videos on it. Thanks!
Do players move in a straight line?
Yes, this is a glass bridge obby
You would get the player’s X or Z coordinates, then set their image position based off of those coordinates.
-- Prize object
local prize = game.Workspace.Prize
-- Create a screen GUI
local playerGui = game.Players.LocalPlayer.PlayerGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- Create a frame for the length bar
local lengthBar = Instance.new("Frame")
lengthBar.Size = UDim2.new(0, 200, 0, 20)
lengthBar.Position = UDim2.new(0.5, -100, 0.9, 0)
lengthBar.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
lengthBar.BorderSizePixel = 2
lengthBar.Parent = screenGui
-- Create an image label for the player's icon
local playerIcon = Instance.new("ImageLabel")
playerIcon.Size = UDim2.new(0, 20, 0, 20)
playerIcon.Position = UDim2.new(0, 0, 0, 0)
playerIcon.Parent = lengthBar
-- Function to update the length bar and player icon
local function updateLengthBar()
-- Get the player's distance from the prize
local player = game.Players.LocalPlayer
local distance = (player.Character.HumanoidRootPart.Position - prize.Position).magnitude
-- Calculate the width of the length bar based on the player's distance
local maxWidth = lengthBar.Size.X.Offset
local width = maxWidth * (distance / 100)
lengthBar.Size = UDim2.new(0, width, 0, 20)
-- Calculate the number of player icons to show on the bar
local iconCount = math.floor(width / playerIcon.Size.X.Offset)
-- Remove any previous player icons on the bar
for _, child in ipairs(lengthBar:GetChildren()) do
if child:IsA("ImageLabel") and child ~= playerIcon then
child:Destroy()
end
end
-- Add new player icons to the bar
for i = 1, iconCount do
local newIcon = playerIcon:Clone()
newIcon.Position = UDim2.new(0, i * playerIcon.Size.X.Offset, 0, 0)
newIcon.Parent = lengthBar
end
end
-- Call the updateLengthBar function every frame
game:GetService("RunService").RenderStepped:Connect(function()
updateLengthBar()
end)
Here’s an explanation of the code:
First, we define a variable named Prize and set its value to the location of the prize in the game world. This will be used later to calculate the distance between the player and the prize.
Next, we define a function called UpdateLengthBar that takes one argument, player. This function will be called every frame to update the length bar for the specified player.
Inside the UpdateLengthBar function, we calculate the distance between the player and the prize using the magnitude function of the Vector3 class. The magnitude function calculates the distance between two points in 3D space.
We then calculate the maximum distance between the player and the prize. This will be used later to determine how much of the length bar should be filled.
We create a variable named length and set its value to the distance between the player and the prize divided by the maximum distance.
Next, we create a UI element called a Frame and set its size and position on the screen. We also set its color and transparency to make it look like a length bar.
We then create another UI element called an ImageLabel and set its position on the screen based on the position of the Frame. We also set its image to the player’s logo icon and its size to a fraction of the Frame’s size, depending on the value of length.
Finally, we parent the ImageLabel to the Frame so that it moves along with the length bar.
We call the UpdateLengthBar function for each player in the game using a for loop.
Overall, this script uses the distance between the player and the prize to create a length bar that displays the player’s logo icon and changes length based on the distance. This can be a helpful visual cue for players to see how close they are to the prize.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.