Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make a weapon hitbox that does constant damage over time?

Asked by 1 year ago

Alright so I got this tool right? I'm trying to make it so that when its activated if a player/enemy is in a certain distance it'll make them take damage constantly.

I've tried using things like Ontouch and Touched events but they don't seem to be working properly. They kinda just damage them once and never again unless you reactivate the tool.

Heres my current script, it DOES do the damage just not constantly like I need it to

01local DebounceTable = {}
02 
03script.Parent.Parent:WaitForChild("HITBOX").Touched:Connect(function(objectThatTouchesTheHitbox)
04    if objectThatTouchesTheHitbox.Parent then
05        if objectThatTouchesTheHitbox.Parent:FindFirstChild("Humanoid") then
06            if DebounceTable[objectThatTouchesTheHitbox.Parent] == true then return end
07            DebounceTable[objectThatTouchesTheHitbox.Parent] = true
08            objectThatTouchesTheHitbox.Parent.Humanoid:TakeDamage(10)
09            wait(0.5)
10            DebounceTable[objectThatTouchesTheHitbox.Parent] = nil
11        end
12    end
13end)

Some help on this would be much appreciated thankss

2 answers

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
1 year ago

You already have half the code, just use your table in an loop Something like:

01local DebounceTable = {}
02 
03local StartTime = tick()
04local HurtTimeTick = 0.0 -- change this to how quick or slow you want it to hurt them
05while true do
06    local NowTime = tick()
07    local DTime = NowTime - StartTime
08    if(DTime >= HurtTimeTick) then
09        StartTime = tick()
10        for Player,_ in pairs(DebounceTable) do
11            if(Player ~= nil) then
12                Player.Humanoid:TakeDamage(10)
13            end
14        end
15        --print(DTime)
16    end
17    wait()
18end

This is a bit of a hack. But it's a way that would keep track of players/npcs that have contacted the hit box. then your timer in the touch event would remove them after a 0.5 sec delay.

The code in full:

01local DebounceTable = {}
02 
03script.Parent.Parent:WaitForChild("HITBOX").Touched:Connect(function(objectThatTouchesTheHitbox)
04    local PossibleHuman = objectThatTouchesTheHitbox.Parent
05    if objectThatTouchesTheHitbox.Parent then
06        if PossibleHuman:FindFirstChild("Humanoid") then
07            if DebounceTable[PossibleHuman] == true then return end
08            DebounceTable[PossibleHuman] = true
09            --PossibleHuman.Humanoid:TakeDamage(10)
10            wait(0.5) -- change this to change the duration of pain for the humanoid
11            DebounceTable[PossibleHuman] = nil
12        end
13    end
14end)
15 
View all 42 lines...

Hope this helps! :)

0
Ahh alright thanks this helped a ton Creature_Horns 4 — 1y
Ad
Log in to vote
0
Answered by
pwx 1581 Moderation Voter
1 year ago

Rather than using a hitbox, you could use Magnitude.

01local Tool = script.Parent
02local Handle = Tool:WaitForChild('Handle')
03 
04local Players = game:GetService('Players')
05 
06local Dmg = 10 -- damage per rate
07local dmgRate = 1 -- how often it damages a player in seconds
08local minDistance = 30 -- how close someone has to be in order to be damaged
09 
10Tool.Equipped:Connect(function()
11    local Player = Players:GetPlayerFromCharacter(Tool.Parent)
12    local Character = Player.Character
13    if Character then
14        local humanoidRootPart = Character:FindFirstChild('HumanoidRootPart')
15        if humanoidRootPart then
View all 36 lines...

Answer this question