I’m trying to get a dot to tween to a player’s X position, but also max out after a certain distance. For example, the dot’s max coordinates will be X -200 to X 200. If the player’s X coordinate is within this range, the dot will tween across the screen in a progress-bar like fashion. If they are outside this coordinate range, the dot will max out before the end of the screen.
Here is a video example of what I mean:
Here is my code so far:
local dot = script.Parent
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local pos = UDim2.new(player.Name.humanoidRootPart.Position, 0, 0.05, 0)
while humanoidRootPart do
dot:TweenPosition(pos)
wait(0.1)
end
end)
end)
Luckily for you, there’s WorldToScreenPoint which converts a Vector3 world position to a Vector3 screen point (X and Y represent X, Y positions). This can be clamped with the math.clamp() function to fit it in your -200 to 200 range.
You’ll want to wait on the LocalPlayer’s Character to spawn (this needs to be a LocalScript), and then update the dot on Stepped which is a less-blocking event that fires about every frame.
ie.
local RunService = game:GetService("RunService");
local Players = game:GetService("Players");
local player = Players.LocalPlayer;
local character = player.Character or player.CharacterAdded:Wait();
local camera = workspace.CurrentCamera;
local dot = script.Parent;
local function update_character(new_character)
character = new_character; --// Update the character variable to yhe newly spawned
end
local function update_dot()
local root = character and character:FindFirstChild("HumanoidRootPart");
if (root) then --// Ensure there is a HumanoidRootPart
local position, _, on_screen = Camera:WorldToScreenPoint(root.Position);
if (on_screen) then
dot.Position = UDim2.new(0, math.clamp(position.X, -200, 200), 0, 0);
end
end
end
player.CharacterAdded:Connect(update_character);
RunService.Stepped:Connect(update_dot);
local startPos = (startblock).Position
local endPos = (endblock).Position
local character = character argument here
local totalDistance = (startPos - endPos).magnitude
local RunService = game:GetService("RunService")
RunService.Stepped:Connect(function()
--// Reusing Returned's code:
local root = character and character:FindFirstChild("HumanoidRootPart");
if (root) then --// Ensure there is a HumanoidRootPart
local Distance = (root.Position - startPos).magnitude
local distanceDivided = Distance / totalDistance
-- Tween the dot using DistanceDivided as the X Scale argument
end
end)