Skip to content

Commit 2363019

Browse files
fix hitbox not updating properly on CharacterAdded
1 parent b66744e commit 2363019

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

modules/Classes/Entity.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type EntityImpl = {
55
new: (ent: Instance) -> Entity,
66
GetType: (self: Entity) -> string,
77
GetCharacter: (self: Entity) -> Model?,
8+
WaitForCharacter: (self: Entity) -> Model,
89
GetName: (self: Entity) -> string,
910
GetDisplayName: (self: Entity) -> string,
1011
GetPosition: (self: Entity) -> Vector3?,
@@ -26,6 +27,7 @@ Entity.__index = Entity
2627
function Entity.new(entity) return setmetatable({ instance = entity }, Entity) end
2728
function Entity:GetType() return typeof(self.instance) end
2829
function Entity:GetCharacter() return self.instance end
30+
function Entity:WaitForCharacter() return self:GetCharacter() end
2931
function Entity:GetName() return tostring(self:GetCharacter()) end
3032
function Entity:GetDisplayName() return self:GetName() end
3133
function Entity:GetPosition()
@@ -52,7 +54,7 @@ function Entity:GetTeamColor() return Color3.fromRGB(255, 255, 255) end
5254

5355
function Entity:isDead()
5456
local humanoid = self:GetHumanoid()
55-
return if humanoid then humanoid:GetState() == Enum.HumanoidStateType.Dead else true
57+
return if humanoid then humanoid:GetState() == Enum.HumanoidStateType.Dead or humanoid.Health <= 0 else true
5658
end
5759
function Entity:isFFed()
5860
local character = self:GetCharacter()

modules/Classes/Player.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ end
1717
function basePlayer:GetName() return self.instance.Name end
1818
function basePlayer:GetDisplayName() return self.instance.DisplayName end
1919
function basePlayer:GetCharacter() return self.instance.Character end
20+
function basePlayer:WaitForCharacter() return self:GetCharacter() or self.instance.CharacterAdded:Wait() end
2021
function basePlayer:GetTeam() return self.instance.Team end
2122
function basePlayer:GetTeamColor() return self.instance.TeamColor.Color end
2223

modules/HitboxHandler.lua

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ local hitboxHandler = {
1313
hitboxTransparency = 0,
1414
hitboxCanCollide = false,
1515
customPartName = "HeadHB",
16-
hitboxPartList = {},
16+
hitboxPartList = {} :: { [string]: boolean },
1717

1818
ignoreTeammates = false,
1919
ignoreFF = false,
2020
ignoreSitting = false,
2121
ignoreSelectedPlayers = false,
22-
ignorePlayerList = {},
22+
ignorePlayerList = {} :: { string },
2323
ignoreSelectedTeams = false,
24-
ignoreTeamList = {},
24+
ignoreTeamList = {} :: { string },
2525
}
2626

2727
type Entity = typeof(require("./Classes/Entity.lua").new(Instance.new("Model"))) & {
@@ -73,7 +73,7 @@ local function addEntity(entity: Entity)
7373
return if hitboxHandler.extendHitbox then hitboxHandler.hitboxCanCollide else value
7474
end))
7575

76-
-- properties don't trigger sethooks when set from a serverscript
76+
-- properties don't trigger sethooks when set by a serverscript
7777
partDumpster:dump(part.Changed:Connect(function(property)
7878
if partProperties.debounce then return end
7979
if partProperties[property] then partProperties[property] = part[property] end
@@ -187,46 +187,57 @@ local function addEntity(entity: Entity)
187187
end
188188
end
189189

190-
local function addUpdateEvents(character: Model?)
191-
--print("addUpdateEvents:", entity:GetName(), character)
192-
if not character then
193-
--print("character not found")
194-
return
195-
end
196-
-- Roblox still hasn't fixed CharacterAdded firing before all of the limbs are loaded
190+
local function addUpdateEvents()
191+
local character = entity:WaitForCharacter()
192+
--print("addUpdateEvents:", entity:GetName())
193+
-- Roblox still hasn't fixed CharacterAdded firing too early
197194
-- https://devforum.roblox.com/t/avatar-loading-event-ordering-improvements/269607
198195
local humanoid
199196
local loaded = false
200197
local startTime = tick()
201198
while not loaded and tick() - startTime <= 2 do
202199
task.wait()
203200
--print("addUpdateEvents loop")
204-
for name, _ in hitboxHandler.hitboxPartList do
205-
--print("checking part", name .. ":", character:FindFirstChild(name) ~= nil)
206-
if not character:FindFirstChild(name) then return end
207-
end
201+
-- I sure hope limbs loading before the humanoid is consistent behavior! Seems fine in my 3 minutes of testing.
208202
humanoid = character:FindFirstChildWhichIsA("Humanoid")
209203
--print("checking humanoid:", humanoid ~= nil)
210-
if not humanoid then return end
204+
if not humanoid then continue end
211205
loaded = true
212206
end
213207
if humanoid then
208+
-- Have to check both Health and StateType since some games disable HumanoidStateType.Dead
209+
-- This has a side effect of calling hitboxStep twice on death for most games. Too bad!
214210
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
215-
if humanoid.Health <= 0 then entity:hitboxStep() end
216-
--print(entity:GetName(), "died")
211+
if humanoid.Health <= 0 then
212+
--print("0Health:", entity:GetName())
213+
entity:hitboxStep()
214+
end
217215
end)
218216
humanoid.StateChanged:Connect(function(_, newState)
219-
if newState == Enum.HumanoidStateType.Dead then entity:hitboxStep() end
217+
if newState == Enum.HumanoidStateType.Dead then
218+
--print("HumanoidDead:", entity:GetName())
219+
entity:hitboxStep()
220+
end
220221
end)
221222
end
222-
character.ChildAdded:Connect(function(child)
223-
if child:IsA("ForceField") then entity:hitboxStep() end
224-
--print(entity:GetName(), "invulnerable")
225-
end)
226-
character.ChildRemoved:Connect(function(child)
227-
if child:IsA("ForceField") then entity:hitboxStep() end
228-
--print(entity:GetName(), "vulnerable")
229-
end)
223+
entity.dumpsters[character] = Dumpster.new()
224+
local characterConnectionsDumpster = entity.dumpsters[character]
225+
characterConnectionsDumpster:dump(character.ChildAdded:Connect(function(child)
226+
if child:IsA("ForceField") then
227+
--print("+forcefield:", entity:GetName())
228+
entity:hitboxStep()
229+
end
230+
end))
231+
characterConnectionsDumpster:dump(character.ChildRemoved:Connect(function(child)
232+
if child:IsA("ForceField") then
233+
--print("-forcefield:", entity:GetName())
234+
entity:hitboxStep()
235+
end
236+
end))
237+
characterConnectionsDumpster:dump(character.AncestryChanged:Connect(function(_, parent)
238+
if parent ~= nil then return end
239+
characterConnectionsDumpster:burn()
240+
end))
230241
entity:hitboxStep()
231242
end
232243

@@ -240,7 +251,7 @@ local function addEntity(entity: Entity)
240251
playerConnectionDumpster:dump(player:GetPropertyChangedSignal("Team"):Connect(entity.hitboxStep))
241252
end
242253

243-
addUpdateEvents(entity:GetCharacter())
254+
addUpdateEvents()
244255
end
245256
local function removeEntity(entity: Entity)
246257
entity.oldProperties = {}
@@ -249,7 +260,7 @@ local function removeEntity(entity: Entity)
249260
end
250261
end
251262

252-
function hitboxHandler:updatePartList(list: { [string]: boolean })
263+
function hitboxHandler:updatePartList(list: { string })
253264
hitboxHandler.hitboxPartList = {}
254265
local partMap: { [string]: { string } } = {
255266
["Custom Part"] = { hitboxHandler.customPartName },
@@ -270,7 +281,7 @@ function hitboxHandler:updatePartList(list: { [string]: boolean })
270281
end
271282
end
272283
function hitboxHandler:updateHitbox()
273-
for _, player in EntHandler:GetPlayers() do
284+
for _, player: Entity in EntHandler:GetPlayers() do
274285
player:hitboxStep()
275286
end
276287
end

modules/Overrides.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ if override then
1515
return true
1616
end
1717
return false
18+
19+
-- TODO: figure out how the hell I'm gonna make this work with the new additions to HitboxHandler.lua

0 commit comments

Comments
 (0)