Skip to content

Commit c30ade8

Browse files
committed
changed hitbox visualization to only send the hrp's cframe instead of every hitbox's position to save some bandwidth
1 parent 37b0b6f commit c30ade8

4 files changed

Lines changed: 16 additions & 98 deletions

File tree

‎DysymmetricalExamplePlace.rbxl‎

-1.79 KB
Binary file not shown.

‎src/ServerScriptService/Managers/PlayerManager/CharacterManager/PlayerHurtboxCreator/init.luau‎

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ function PlayerHurtboxCreator:Init()
2222
end
2323

2424
function PlayerHurtboxCreator.CreateHurtboxesForPlayer(Player: Player, Character: Model)
25-
local ManagedHurtboxes = {}
26-
2725
local HRP = Utils.Character.GetRootPart(Character)
2826
local TargetCFrame = CFrame.new(HRP.CFrame.Position)
2927

@@ -69,19 +67,16 @@ function PlayerHurtboxCreator.CreateHurtboxesForPlayer(Player: Player, Character
6967
Weld.Parent = HRP
7068

7169
--parent it NOW instead of BEFORE so that the client can properly get the ID attribute
72-
HitboxClone.Parent = Character.Hitboxes
73-
74-
--add it to the table for later hitbox display
75-
ManagedHurtboxes[ID] = HitboxClone
70+
HitboxClone.Parent = Character.Hurtboxes
7671
end
7772

7873
--setup hitbox display on client
79-
PlayerHurtboxCreator._SetupHurtboxViewConnections(Player, Character, ManagedHurtboxes)
74+
PlayerHurtboxCreator._SetupHurtboxViewConnections(Player, Character)
8075
end
8176

8277
local Framerate = 1/15
8378
--- Sets up all available hitboxes for the passed character.
84-
function PlayerHurtboxCreator._SetupHurtboxViewConnections(Player: Player, Character: Model, ManagedHitboxes: {[number]: BasePart})
79+
function PlayerHurtboxCreator._SetupHurtboxViewConnections(Player: Player, Character: Model)
8580
local JanitorInstance = Janitor.new()
8681
JanitorInstance:LinkToInstance(Character)
8782

@@ -112,59 +107,9 @@ function PlayerHurtboxCreator._SetupHurtboxViewConnections(Player: Player, Chara
112107

113108
LastPosition = CurrentPosition
114109

115-
--get every single hitbox that still exists from the managed hitboxes JUST IN FUCKING CASE.
116-
local AliveHitboxes = {}
117-
local Count = 0
118-
for ID: number, Hitbox: BasePart in ManagedHitboxes do
119-
if not (Hitbox and Hitbox.Parent) then
120-
continue
121-
end
122-
123-
Count += 1
124-
AliveHitboxes[ID] = Hitbox
125-
end
126-
127-
--if there are no available hitboxes to update, halt
128-
if Count <= 0 then
129-
return
130-
end
131-
132-
--let me break this down
133-
-- * header for the hitbox count (1 byte, so you can add up to 255 hitboxes)
134-
-- * multiplying the amount of hitboxes by 13
135-
-- * 1 byte for the ID
136-
-- * 12 bytes for XYZ
137-
--cool size done!!1!
138-
local BufferSize = 1 + (Count * 13)
139-
140-
--we make the buffer
141-
local Buffer = buffer.create(BufferSize)
142-
--skipping the count byte because that's set by itself in an obvious manner below
143-
local Offset = 1
144-
145-
--write count as the header
146-
buffer.writeu8(Buffer, 0, Count)
147-
148-
--iterate over every alive hitbox to add it to the buffer
149-
for ID: number, Hitbox: BasePart in AliveHitboxes do
150-
--write its id as its header
151-
buffer.writeu8(Buffer, Offset, ID :: any)
152-
--move forward 1 byte
153-
Offset += 1
154-
155-
--iterate over every axis from its position
156-
local Position = Hitbox.CFrame.Position
157-
for _, Axis in {"X", "Y", "Z"} do
158-
--write it as a 32 bit float, which should be more than enough (4 bytes, up to 7 decimals)
159-
buffer.writef32(Buffer, Offset, Position[Axis])
160-
161-
--move forward 4 bytes
162-
Offset += 4
163-
end
164-
end
165-
166-
--send it to the client to update
167-
Network:FireClientConnection(Player, "UpdHurtboxPos", "UREMOTE_EVENT", Buffer)
110+
--sending the root's cframe directly to update the hurtboxes's cframe on the client
111+
--i can send the cframe because it'll still be optimized and buffered by the networking module anyways
112+
Network:FireClientConnection(Player, "UpdHurtboxPos", "UREMOTE_EVENT", Root.CFrame)
168113
end))
169114
end
170115

‎src/StarterCharacterScripts/PlayerAttributeManagers/SpeedManager/SlowOnFall.luau‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ local SlowOnFall = {}
77

88
function SlowOnFall:Init()
99
local Character = Players.LocalPlayer.Character
10+
if Character:GetAttribute("Role") == "Killer" then
11+
return
12+
end
13+
1014
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
1115
local Root = Utils.Character.GetRootPart(Character)
1216

‎src/StarterCharacterScripts/Visual/HurtboxVisualizer.luau‎

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ return {
1313

1414
--necessary tables
1515
local GhostHurtboxes: {[number]: BasePart} = {}
16-
local TargetPositions: {[number]: Vector3} = {}
16+
local TargetCFrame: CFrame = Utils.Character.GetRootPart(Character).CFrame
1717

1818
--creating the janitor by conveniently using `Utils.Instance.ObserveChildren()`, which is bound to HurtboxFolder
1919
local JanitorInstance: Types.Janitor = Utils.Instance.ObserveChildren(HurtboxFolder, function(Child: Instance)
@@ -28,9 +28,6 @@ return {
2828
return
2929
end
3030

31-
--add its CURRENT position to the table just in case
32-
TargetPositions[ID] = Child.CFrame.Position
33-
3431
--create its dupe with its supposed transparency
3532
local Ghost = Child:Clone()
3633
Ghost.Parent = HurtboxFolder
@@ -59,46 +56,18 @@ return {
5956
GhostHurtboxes[ID]:Destroy()
6057
end
6158
GhostHurtboxes[ID] = nil
62-
TargetPositions[ID] = nil
6359
end))
6460

6561
--use the same janitor again to add the network connection
66-
JanitorInstance:Add(Network:SetConnection("UpdHurtboxPos", "UREMOTE_EVENT", function(Buffer: buffer)
67-
--start from 1 to skip the buffer's counter header at first
68-
local Offset = 1
69-
70-
local Count = buffer.readu8(Buffer, 0)
71-
72-
--iterate over the amount of hurtboxes
73-
for _ = 1, Count do
74-
--get the id and increase the offset
75-
local ID = buffer.readu8(Buffer, Offset)
76-
Offset += 1
77-
78-
--read every axis and write it to a table for creating the vector
79-
local AxisTable: {[string]: number} = {}
80-
for _, Axis in {"X", "Y", "Z"} do
81-
AxisTable[Axis] = buffer.readf32(Buffer, Offset)
82-
Offset += 4
83-
end
84-
85-
--set the new vector with the set values to the set ID
86-
TargetPositions[ID] = Vector3.new(AxisTable.X, AxisTable.Y, AxisTable.Z)
87-
end
62+
JanitorInstance:Add(Network:SetConnection("UpdHurtboxPos", "UREMOTE_EVENT", function(_TargetCFrame: CFrame)
63+
TargetCFrame = _TargetCFrame
8864
end))
8965

9066
--use the same janitor yet again to add the render loop
9167
JanitorInstance:Add(RunService.PreRender:Connect(function(delta: number)
92-
--iterate over every available position in this frame
93-
for ID: number, targetPosition: Vector3 in TargetPositions do
94-
--if a ghost for this ID doesn't exist, move on to the next one
95-
local Ghost = GhostHurtboxes[ID]
96-
if not Ghost then
97-
continue
98-
end
99-
100-
--lerp the ghost's position to its supposed target position
101-
Ghost.CFrame = CFrame.new(Ghost.Position:Lerp(targetPosition, delta * 20))
68+
for _, Ghost in GhostHurtboxes do
69+
Ghost.CFrame = Ghost.CFrame:Lerp(TargetCFrame, delta * 20)
70+
-- Ghost.CFrame = TargetCFrame
10271
end
10372
end))
10473
end,

0 commit comments

Comments
 (0)