keexers

Gunfight Arena Script (FOR EDUCATION PURPOSES)

Jan 4th, 2025
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | Gaming | 0 0
  1. -- services
  2. local players = game:GetService("Players");
  3. local teams = game:GetService("Teams");
  4.  
  5. -- variables
  6. local camera = workspace.CurrentCamera;
  7. local get_closest_player = function()
  8.     local closest = nil;
  9.     local closest_distance = math.huge;
  10.  
  11.     for _, character in workspace.GetChildren(workspace) do
  12.         local player = players.FindFirstChild(players, character.Name);
  13.         local root_part = character.FindFirstChild(character, "HumanoidRootPart");
  14.  
  15.         if (not player) or (not root_part) then
  16.             continue;
  17.         end
  18.  
  19.         local team_attribute = player.GetAttribute(player, "Team");
  20.  
  21.         if (not team_attribute) then
  22.             continue;
  23.         end
  24.  
  25.         if (teams[team_attribute] == players.LocalPlayer.Team) then
  26.             continue;
  27.         end
  28.  
  29.         local position, on_screen = camera.WorldToViewportPoint(camera, root_part.Position);
  30.  
  31.         if (not on_screen) then
  32.             continue;
  33.         end
  34.  
  35.         local center = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2);
  36.         local distance = (Vector2.new(position.X, position.Y) - center).Magnitude;
  37.  
  38.         if (closest_distance > distance) then
  39.             closest = character;
  40.             closest_distance = distance;
  41.         end
  42.     end
  43.  
  44.     return closest;
  45. end
  46.  
  47. -- main
  48. do
  49.     local events = { -- only a table incase you wanna add more events, its really easy
  50.         ["ShootEvent"] = function(arg)
  51.             return (typeof(arg) == "Instance" and arg.Name and (string.find(arg.Name, players.LocalPlayer.Name))); -- dumb method for checking if the shooter is lplr, but none of the other ones seemed to work... (?, might've been tweaking, feel free to try it)
  52.         end,
  53.     };
  54.  
  55.     old_namecall = hookmetamethod(game, "__namecall", function(self, caller, message, ...)
  56.         local method = getnamecallmethod();
  57.  
  58.         if (method == "Fire" and self.Name == "Sync") then -- intercept actor communication for 1337 haxx.. (so we can log all events being sent to the actor)
  59.             for event, identify in events do
  60.                 if (event == "ShootEvent" and identify(message)) then
  61.                     local closest_player = get_closest_player();
  62.                     local ammo, cframe, id, weapon, projectile = ...;
  63.  
  64.                     if (closest_player and closest_player.FindFirstChild(closest_player, "Head")) then
  65.                         cframe = closest_player.Head.CFrame; -- HEAT manipulation...
  66.                     end
  67.  
  68.                     return old_namecall(self, caller, message, ammo, cframe, id, weapon, projectile, ...);
  69.                 end
  70.             end
  71.         end
  72.  
  73.         return old_namecall(self, caller, message, ...);
  74.     end)
  75. end
Add Comment
Please, Sign In to add comment