Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
- local Components = require(ReplicatedStorage.Shared.Components)
- -- i dunno what values i should set yet
- local TICK_LENGTH = 10/60
- local MAX_SPEED_THRESHOLD = 000
- local MAX_PING_THRESHOLD = 000
- local function ReplicatePlayerTransforms(world, state)
- -- runs for each newly added player
- for id, transform, player in world:query(Components.Transform, Components.Player) do
- if player.OldPosition then
- continue
- end
- world:insert(id, player:patch({
- OldPosition = transform.Position,
- }))
- end
- if Matter.useThrottle(TICK_LENGTH) then
- for id, transform, player in world:query(Components.Transform, Components.Player) do
- local oldPlayer = player
- local oldPosition = player.OldPosition
- local currentPosition = transform.Position
- player = player:patch({
- OldPosition = currentPosition,
- })
- world:insert(id, player)
- local ping = player.Instance:GetNetworkPing()
- local delta = oldPosition - currentPosition
- local speed = delta.Magnitude / (TICK_LENGTH + ping / 2)
- if speed <= MAX_SPEED_THRESHOLD and ping <= MAX_PING_THRESHOLD then
- continue
- end
- world:insert(id, oldPlayer, transform:patch({
- Position = oldPosition
- }))
- end
- end
- for _, instance, position, rotation in Matter.useEvent(ReplicatedStorage.Shared.Remotes.SendPlayerPosition, "OnServerEvent") do
- local id = instance:GetAttribute("EntityId")
- if not id or not world:contains(id) then
- continue
- end
- local player = world:get(id, Components.Player)
- local transform = world:get(id, Components.Transform)
- if not player or not transform then
- continue
- end
- world:insert(id,
- transform:patch({
- Position = position,
- Rotation = rotation,
- DoNotReplicate = true,
- }),
- player:patch({
- RealPosition = position,
- RealRotation = rotation,
- })
- )
- end
- end
- return ReplicatePlayerTransforms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement