Advertisement
AlewAlow

pls

Sep 7th, 2023 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3.  
  4. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  5. local Components = require(ReplicatedStorage.Shared.Components)
  6.  
  7. -- i dunno what values i should set yet
  8. local TICK_LENGTH = 10/60
  9. local MAX_SPEED_THRESHOLD = 000
  10. local MAX_PING_THRESHOLD = 000
  11.  
  12. local function ReplicatePlayerTransforms(world, state)
  13.     -- runs for each newly added player
  14.     for id, transform, player in world:query(Components.Transform, Components.Player) do
  15.         if player.OldPosition then
  16.             continue
  17.         end
  18.        
  19.         world:insert(id, player:patch({
  20.             OldPosition = transform.Position,
  21.         }))
  22.     end
  23.    
  24.     if Matter.useThrottle(TICK_LENGTH) then
  25.         for id, transform, player in world:query(Components.Transform, Components.Player) do
  26.             local oldPlayer = player
  27.             local oldPosition = player.OldPosition
  28.             local currentPosition = transform.Position
  29.            
  30.             player = player:patch({
  31.                 OldPosition = currentPosition,
  32.             })
  33.            
  34.             world:insert(id, player)
  35.            
  36.             local ping = player.Instance:GetNetworkPing()
  37.             local delta = oldPosition - currentPosition
  38.             local speed = delta.Magnitude / (TICK_LENGTH + ping / 2)
  39.             if speed <= MAX_SPEED_THRESHOLD and ping <= MAX_PING_THRESHOLD then
  40.                 continue
  41.             end
  42.            
  43.             world:insert(id, oldPlayer, transform:patch({
  44.                 Position = oldPosition
  45.             }))
  46.         end
  47.     end
  48.    
  49.     for _, instance, position, rotation in Matter.useEvent(ReplicatedStorage.Shared.Remotes.SendPlayerPosition, "OnServerEvent") do
  50.         local id = instance:GetAttribute("EntityId")
  51.         if not id or not world:contains(id) then
  52.             continue
  53.         end
  54.        
  55.         local player = world:get(id, Components.Player)
  56.         local transform = world:get(id, Components.Transform)
  57.         if not player or not transform then
  58.             continue
  59.         end
  60.        
  61.         world:insert(id,
  62.             transform:patch({
  63.                 Position = position,
  64.                 Rotation = rotation,
  65.                 DoNotReplicate = true,
  66.             }),
  67.             player:patch({
  68.                 RealPosition = position,
  69.                 RealRotation = rotation,
  70.             })
  71.         )
  72.     end
  73. end
  74.  
  75. return ReplicatePlayerTransforms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement