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
01 | local DebounceTable = { } |
02 |
03 | script.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 |
13 | end ) |
Some help on this would be much appreciated thankss
You already have half the code, just use your table in an loop Something like:
01 | local DebounceTable = { } |
02 |
03 | local StartTime = tick() |
04 | local HurtTimeTick = 0.0 -- change this to how quick or slow you want it to hurt them |
05 | while 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() |
18 | end |
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:
01 | local DebounceTable = { } |
02 |
03 | script.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 |
14 | end ) |
15 |
Hope this helps! :)
Rather than using a hitbox, you could use Magnitude.
01 | local Tool = script.Parent |
02 | local Handle = Tool:WaitForChild( 'Handle' ) |
03 |
04 | local Players = game:GetService( 'Players' ) |
05 |
06 | local Dmg = 10 -- damage per rate |
07 | local dmgRate = 1 -- how often it damages a player in seconds |
08 | local minDistance = 30 -- how close someone has to be in order to be damaged |
09 |
10 | Tool.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 |