i’m working on a bossfighting game and most of the bosses attacks use parts named “Orb”, but i’m not sure what the best way is to detect if they’ve been touched. i’m using client sided hit detection because i believe it’s more accurate so all of the hit detection stuff is being done in a local script.
this is a local script stored inside of a folder called “HitDetection” inside of the characters model
character = script.Parent.Parent
root = character:WaitForChild("HumanoidRootPart")
closestHit = nil
debounce = false
closest = nil
function hit(orb)
script.Parent.Hit:FireServer(orb)
closestHit = orb
local closestDebounce = coroutine.create(function()
debounce = true
task.wait(0.25)
debounce = false
end)
coroutine.resume(closestDebounce)
end
while task.wait() do
closest = nil
for i, v in ipairs(workspace.PartsToFilter:GetChildren()) do
local distance = math.huge
if v.Name == "Orb" then
if (v.Position - root.Position).Magnitude < distance then
distance = (v.Position - root.Position).Magnitude
closest = v
end
end
end
if closest then
closest.Touched:Connect(function(part)
local touchedCharacter = part:FindFirstAncestorWhichIsA("Model")
if touchedCharacter then
if touchedCharacter:FindFirstChild("Humanoid") then
if closest == closestHit then
if debounce == false then
hit(closest)
end
else
hit(closest)
end
end
end
end)
end
end
the damage is being done through a server script.
this script does work but i assume it would probably lag a lot and there would be way better ways of writing it
I am not entirely sure why you are checking the closest part like that, feels a bit overcomplicated.
I would connect the following to every orb’s .Touched event.
orb.Touched:Connect(function(touchedPart)
local touchedPlayer = game.Players:GetPlayerFromCharacter(touchedPart.Parent)
if player then
--You know that the orb touched a player, do anything with that player
end
end)
You could also do it the other way around and have a script on the player that checks if a part of the player touches a part called “Orb”. But this would be triggered by anything called “Orb”, so maybe doing the checks on the orbs themselves would be more reliable.
i’m probably wrong, but since .touched waits until the part has been touched, i thought doing lots of .touched events would decrease performance a lot.
I’d say it would only be a problem if you had a very big amount of orbs.
If this is the case and you don’t want to have a lot of touched events, you could make a single one on the player’s HumanoidRootPart like I described in the latter part of my first response.
Okay if you’re rapidly creating and destroying 100 orbs at a time, connecting a .Touched to every single one might be pretty performance heavy.
But if you do it the other way that I mentioned above it should definitely not be an issue performance wise.
oh wait i’m really stupid, i didn’t read the 2nd part of the post correctly, i never thought of doing a touched event on the characters parts. i wouldn’t just do it on the humanoid root part only, i’d loop through all the characters parts instead since the root doesn’t cover the whole character. thanks for the help