while task.wait(0.001) do
local debris = game:GetService('Debris')
local debounce = false
if script.Parent.Triggered.Value == true and debounce == false then
local high = 0
local low = 999999999999
local enemy = script.Sort
local enemies = workspace.Enemies
local waitTime = 0.01 -- time to wait, in seconds
-- create a new task
for i, v in pairs(enemies:GetChildren()) do
local distance = (v.Torso.Position - script.Parent.Position).Magnitude
if distance < 99 and debounce == false and script.Parent.Triggered == true then
v.Zombie.Health =- 30
--script.Parent.ExplosionEffect.Enabled = true
script.Parent.Transparency = 1
-- script.Parent.Explosion.Visible = true
script.Parent.EXP_Triggered.Value = true
script.Parent.Triggered.Value = false
print('Dealt 30 DMG to ', v.Zombie.Name)
debounce = true
end
--script.Parent.ExplosionEffect.Enabled = true
--script.Parent.explode:Play()
print('explosionradius')
script.Disabled = true
--script.Parent:Destroy()
end
else
end
end
this is a projectile script in my TD (tower defense) game, and somehow its not even printing ‘‘Dealt 30 Damage to target’’ it only prints
‘‘explosionradius’’
is there anything wrong with my script?
(also my tower script activates the triggered value to be true, so don’t say ‘‘you forgot to activate the triggered value’’)
That’s cause you’re looping way too often which is resulting in the code firing like, 1/1,000th’s of a second which doesn’t even work to begin with as task.wait()'s yielding duration is the equivalent of RunService’s Heartbeat (Or 1/40th’s of a second max)
You’re also setting your debounce variable to true the moment it loops once, and then it doesn’t switch back to false once the loop finishes to begin again which prevents your pairs function from running where you should just honestly not have it to begin with, and replace your while task.wait() do loop with a while true do loop instead, and call task.wait(waitTime) from there
Now I have no idea on how you have this set up, but I configured your code a tad bit so that you can hopefully get a bit more understanding on how the code is supposed to work from what I see:
local debris = game:GetService('Debris')
local enemies = workspace:WaitForChild("Enemies")
local enemy = script.Sort
local high = 0
local low = 999999999999
local waitTime = 0.25 -- time to wait, in seconds
while true do
if script.Parent.Triggered.Value == true then
-- create a new task
for i, v in pairs(enemies:GetChildren()) do
local distance = (v.Torso.Position - script.Parent.Position).Magnitude
if distance < 99 and script.Parent.Triggered.Value == true then
v.Zombie.Health =- 30
--script.Parent.ExplosionEffect.Enabled = true
script.Parent.Transparency = 1
-- script.Parent.Explosion.Visible = true
script.Parent.EXP_Triggered.Value = true
--script.Parent.Triggered.Value = false
print('Dealt 30 DMG to', v.Zombie.Name)
end
--script.Parent.ExplosionEffect.Enabled = true
--script.Parent.explode:Play()
print('explosionradius')
--script.Disabled = true
--script.Parent:Destroy()
end
else
warn("Script is currently not enabled!")
end
task.wait(waitTime)
end
1: We define our variables at the start so that we don’t have to keep re-defining them per loop
2: The while true do loop begins
3: It checks if script.Parent.Triggered.Value is equal to true to begin the distance loop, if it doesn’t then it returns back with a warn in our console output
4: We check the distance if it’s properly valid & script.Parent.Triggered.Value again, and damage our selected Enemy if the conditions are met
5: Call task.wait(waitTime) before the loop ends at a reasonable rate
yeah but is it a way so it just deals damage just singly? so instead OF looping damage to the target, it just filters it after a single hit, INSTEAD of looping the damage.
You could just encase it into a function, so that when you can call it whenever it’s necessary
I also noticed that you have a BoolValue within your code, and I’d personally recommend using the .Changed Event of that object so that way you don’t have to keep frequently looping, and it can detect when it should activate at the proper time when you want it to
The parameter of .Changed for Value Objects is always the updated Value, so you don’t have to worry about calling BoolValue.Value every so often:
local debris = game:GetService('Debris')
local enemies = workspace:WaitForChild("Enemies")
local enemy = script.Sort
local Triggered = script.Parent:WaitForChild("Triggered")
local high = 0
local low = 999999999999
local waitTime = 0.25 -- time to wait, in seconds
local function DealDamage()
for i, v in pairs(enemies:GetChildren()) do
local distance = (v.Torso.Position - script.Parent.Position).Magnitude
if distance < 99 and script.Parent.Triggered.Value == true then
v.Zombie.Health -= 30
--script.Parent.ExplosionEffect.Enabled = true
script.Parent.Transparency = 1
-- script.Parent.Explosion.Visible = true
script.Parent.EXP_Triggered.Value = true
--script.Parent.Triggered.Value = false
print('Dealt 30 DMG to', v.Zombie.Name)
end
--script.Parent.ExplosionEffect.Enabled = true
--script.Parent.explode:Play()
print('explosionradius')
--script.Disabled = true
--script.Parent:Destroy()
end
end
local function CheckActivation(NewVal)
if NewVal == true then
DealDamage()
end
end
Triggered.Changed:Connect(CheckActivation)
CheckActivation(Triggered.Value)
We’ve connected our Triggered object to a Changed Event, which will check if it’s equal to true or not and if it is then we can call CallDamage() which damages enemies within the radius you provided it with
You should’ve provide that information at the beginning so that I would’ve understood your case more clearer, cause we’re just slowly working towards every piece of information you’re trying to give me here
You can just create a table of AlreadyTagged Enemies, so that when the loop goes through everything you can use table.find to check if that Enemy has already been hit, and it’ll return back either the index it was found at, or nil and if it is not stored in the table, we can use table.insert to insert that chosen object in the table:
local AlreadyTagged = {}
local TagCheck = table.find(AlreadyTagged, ObjectToTagHere)
if TagCheck == nil then
table.insert(TagCheck, ObjectToTagHere)
else
print("Target has been tagged already")
end
Keep in mind I’m giving out an example here, if you want it within your DealDamage function you’ll have to reference the TagCheck near your distance variable, and do the proper effects when the conditions are met once TagCheck is equal to nil to be able to insert that specific enemy within the table
while task.wait(0.001) do
local debris = game:GetService('Debris')
local debounce = false
if script.Parent.Triggered.Value == true and debounce == false then
local high = 0
local low = 999999999999
local enemy = script.Sort
local enemies = workspace.Enemies
local waitTime = 0.01 -- time to wait, in seconds
-- create a new task
for i, v in pairs(enemies:GetChildren()) do
local distance = (v.Torso.Position - script.Parent.Position).Magnitude
local AlreadyTagged = {}
local TagCheck = table.find(AlreadyTagged, v)
if distance < 99 and debounce == false and script.Parent.Triggered.Value == true and TagCheck == nil then
v.Zombie.Health =- 30
table.insert(TagCheck, v)
--script.Parent.ExplosionEffect.Enabled = true
script.Parent.Transparency = 1
-- script.Parent.Explosion.Visible = true
script.Parent.EXP_Triggered.Value = true
script.Parent.Triggered.Value = false
print('Dealt 30 DMG to ', v.Zombie.Name)
debounce = true
elseif TagCheck ~= nil then
warn('already tagged')
end
--script.Parent.ExplosionEffect.Enabled = true
--script.Parent.explode:Play()
print('explosionradius')
script.Disabled = true
--script.Parent:Destroy()
end
else
end
end
Why are you using that Script? It’s pretty clear that the script you’re currently using does not work all that well
You should be using the script that I provided you, as it efficiently checks when to properly call the functions needed to damage the enemies, although I will say to forget about what I just said earlier on having the table inside the DealDamage function, and to put it outside on step 1:
local debris = game:GetService('Debris')
local enemies = workspace:WaitForChild("Enemies")
local enemy = script.Sort
local Triggered = script.Parent:WaitForChild("Triggered")
local AlreadyTagged = {}
local high = 0
local low = 999999999999
local waitTime = 0.25 -- time to wait, in seconds
local function DealDamage()
for i, v in pairs(enemies:GetChildren()) do
local distance = (v.Torso.Position - script.Parent.Position).Magnitude
local TagCheck = table.find(AlreadyTagged, v)
if distance < 99 and script.Parent.Triggered.Value == true and TagCheck == nil then
table.insert(AlreadyTagged, v)
v.Zombie.Health -= 30
--script.Parent.ExplosionEffect.Enabled = true
script.Parent.Transparency = 1
-- script.Parent.Explosion.Visible = true
script.Parent.EXP_Triggered.Value = true
--script.Parent.Triggered.Value = false
print('Dealt 30 DMG to', v.Zombie.Name)
end
--script.Parent.ExplosionEffect.Enabled = true
--script.Parent.explode:Play()
print('explosionradius')
--script.Disabled = true
--script.Parent:Destroy()
end
end
local function CheckActivation(NewVal)
if NewVal == true then
DealDamage()
end
end
Triggered.Changed:Connect(CheckActivation)
CheckActivation(Triggered.Value)