You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I want my hitbox to detect a character after the target character has died
- What is the issue? Include screenshots / videos if possible!
I’m using a custom module inside of StarterCharacterScripts to create the Hitbox.
At the moment my hitbox perfectly detects the characters, until they die, then it doesnt detect the particular character that respawned, every other character can still be hit.
If I reset after the target character dies, I’m able to hit it again, so my immediate guess is that the issue is something to do with the Module not refreshing its character instances.
- What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I’ve tried looking around DevForum but I havent found anything clear, I did try to reset the character references by making a function that basically took in the parameters for all the variables I used and then sets them to nil so that the next time the variables are declared, it would be with a fresh set of values (but that in itself is just guesswork)
VIDEO OF BUG: Watch Hitbox Bug | Streamable
Here’s the Hitbox Module script inside of StarterCharacterScripts:
local module = {}
local Pathways = require(game.ReplicatedStorage.Modules.Pathways)
local Debris = game:GetService("Debris")
local players = game:GetService("Players")
local hitSet = {}
local hitPartTable = {}
function module.createHitbox(char, cframe, size, overlapParameters)
local hitboxResult = workspace:GetPartBoundsInBox(cframe, size, overlapParameters)
local reference = Instance.new("Part") -- CREATING A PART TO INDICATE THE LOCATION & SIZE OF THE HITBOX
reference.Parent = workspace
reference.CFrame = cframe
reference.Size = size
reference.CanCollide = false
reference.Color = Color3.new(1, 0.0470588, 0.0470588)
reference.Transparency = 0.5
reference.Anchored = true
Debris:AddItem(reference, .2)
if hitboxResult then
if #hitPartTable > 0 then
table.clear (hitPartTable) -- DELETING PREVIOUS STORED CHARACTER(S)
end
for _, part in (hitboxResult) do
if part:IsDescendantOf(char) then
_, part = nil -- IF THE PART BELONGS TO THE PLAYER'S CHARACTER THEN IT GETS SET TO NIL
end
if part ~= nil then -- MAKING SURE THE PLAYER DOESN'T HIT THEMSELVES
if part.Name == "Hitbox" then
print(part.Parent.Parent)
local hitTarg = part.Parent.Parent
table.insert(hitSet, hitTarg)
print (hitSet)
end
for _, targ in hitSet do
if not table.find(hitPartTable, targ) then
table.insert(hitPartTable, targ)
end
table.clear(hitSet)
end
end
end
print (hitPartTable) -- CHECKING THAT THE VALUES STILL EXIST IN THE TABLE
return hitPartTable -- SENDING THE TABLE WITH THE CHARACTER(S) THAT WERE HIT TO THE CLIENT
end
end
function module.createDotHitbox(char, enemies)
----- MODULE FUNCTION THAT CHECKS THAT THE ENEMY CHARACTER IS WITHIN A CERTAIN RANGE AND VIEW OF THE PLAYER -----
if char == nil or enemies == nil then return end
local angle = 45 -- MAX VIEW ANGLE
local distance = 10 -- MAX DISTANCE
for _, enemy in enemies do
local resultantVector = enemy.PrimaryPart.Position - char.PrimaryPart.Position -- GETTING THE DISTANCE AND DIRECTION OF THE ENEMY FROM THE PLAYER
local lookVector = char.PrimaryPart.CFrame.LookVector -- GETTING THE DIRECTION THE PLAYER IS FACING
local normalResult = resultantVector.Unit -- GETTING THE NORMALIZED DIRECTION
local magnitude = resultantVector.Magnitude -- GETTING THE DISTANCE
local Dot = lookVector:Dot(normalResult) -- GETTING THE DOT PRODUCT OF THE PLAYERS LOOKING DIRECTION AND THE DIRECTION OF WHERE THE ENEMY IS
local radianAngle = math.acos(Dot) -- GETTING ANGLE IN RADIANS
local degAngle = math.deg(radianAngle) -- CONVERTING RADIANS TO DEGREES
local inRange = magnitude <= distance
local inView = degAngle <= angle
local canHit = inRange and inView
if not canHit then
warn("You can't hit that.")
return
end
print(char.Name.." has hit, "..enemy.Name)
return enemies
end
end
return module
No errors showing in output btw, just nothing printing at all
Thanks for all the help!