Here is my script
local function GetClosestPlayer() local LowestDistance = math.huge local ClosestCharacter = nil local Characters = {} for i,v in pairs(game:GetService("Players"):GetPlayers()) do if v.Character ~= nil and v ~= game.Players.LocalPlayer then table.insert(Characters, v.Character) end end for i,v in pairs(Characters) do if v ~= nil then if v:FindFirstChild("HumanoidRootPart") then local InitialDis = (v.HumanoidRootPart.Position - game.Workspace.CurrentCamera.CFrame.p).magnitude local Ray = Ray.new(game.Workspace.CurrentCamera.CFrame.p, (mouse.Hit.p - game.Workspace.CurrentCamera.CFrame.p).unit * InitialDis) local Part,Position = game.Workspace:FindPartOnRay(Ray, game.Workspace) local FinalDifference = math.floor((Position - v.HumanoidRootPart.Position).magnitude) if FinalDifference < LowestDistance then ClosestCharacter = v LowestDistance = FinalDifference end end end end local ClosestPlayer = game.Players:GetPlayerFromCharacter(ClosestCharacter) if ClosestPlayer ~= nil then return ClosestPlayer.name end
I want to return ClosestPlayer.name as a string so that I can print their name. Please help!
message = "he said \"bye\" and left" print (message
Please tell me if this answer works. If it does, don't forget to upvote it :P