What do you want to achieve?
I want to try and detect if at least one enemy in a folder has a boolvalue called “Attacking” enabled. Afterwards the current ambience will stop, and a battle ambience will play. If all the values are false or none can be found(due to all enemies being destroyed), then the normal ambience returns.
What is the issue?
It doesn’t detect any boolvalues, and doesn’t play the necessary audio. There are no errors in the output. (Edit: Most of these issues have been fixed. The only remaining issue is that it only gets the value of the first enemy instead of looping through all the enemies’ values.)
What solutions have you tried so far?
I’ve seen almost similar topics of getting boolvalues in pairs, and have attempted to try them, but I’m struggling to grasp, and implement the concept.
This is the updated script after looking through the solutions:
local EnemyFolder = workspace.Attackables
local Ambience = script.AmbienceLente
local BattleMusic = script.VengefulAttack
local ActiveSong = nil
local DeactiveSong = nil
while true do
wait(0.1)
local Enemies = EnemyFolder:GetChildren()
if (#Enemies > 0) then
for i, v in pairs (Enemies) do
if v:IsA("Model") and v.Attacking.Value == true then
ActiveSong = BattleMusic
DeactiveSong = Ambience
elseif not v:IsA("Model") or v.Attacking.Value == false then
ActiveSong = Ambience
DeactiveSong = BattleMusic
end
end
else
ActiveSong = Ambience
DeactiveSong = BattleMusic
end
if DeactiveSong.IsPlaying then
DeactiveSong:Stop()
end
if ActiveSong.IsPaused then
ActiveSong:Play()
end
end
for i, v in pairs(Enemies) do
if v:IsA("Model") and v.Attacking.Value then
ActiveSong = BattleMusic
DeactiveSong = Ambience
break
elseif not v:IsA("Model") or not v.Attacking.Value then
ActiveSong = Ambience
DeactiveSong = BattleMusic
end
end
Thanks, the audios were able to switch, the only issue still is that it only detects the value of the first model in the folder, even if other enemies are attacking.
local EnemyFolder = workspace.Attackables
local Ambience = script.AmbienceLente
local BattleMusic = script.VengefulAttack
Ambience.Loop = true
BattleMusic.Loop = true
local ActiveSong = nil
local DeactiveSong = nil
while true do
local Enemies = EnemyFolder:GetChildren()
local canplay = false
for i, v in pairs(Enemies) do
if v:IsA(“Model”) and v.Attacking.Value then
– ActiveSong = BattleMusic
canplay = true
– DeactiveSong = Ambience
elseif not v:IsA(“Model”) or not v.Attacking.Value then
–ActiveSong = Ambience
–DeactiveSong = BattleMusic
end
end
Ambience.Playing = not canplay
BattleMusic.Playing = canplay
–DeactiveSong:Stop()
–ActiveSong:Play()
wait(0.1)
end
I was able to prevent the sounds from stopping/playing, switch when there are no enemies, and switch when the attacking value is false. This is the updated script:
local EnemyFolder = workspace.Attackables
local Ambience = script.AmbienceLente
local BattleMusic = script.VengefulAttack
local ActiveSong = nil
local DeactiveSong = nil
while true do
wait(0.1)
local Enemies = EnemyFolder:GetChildren()
if (#Enemies > 0) then
for i, v in pairs (Enemies) do
if v:IsA("Model") and v.Attacking.Value then
ActiveSong = BattleMusic
DeactiveSong = Ambience
elseif not v:IsA("Model") or not v.Attacking.Value then
ActiveSong = Ambience
DeactiveSong = BattleMusic
end
end
else
ActiveSong = Ambience
DeactiveSong = BattleMusic
end
if DeactiveSong.IsPlaying then
DeactiveSong:Stop()
end
if ActiveSong.IsPaused then
ActiveSong:Play()
end
end
The only remaining issue is that it only gets the attacking value of the first enemy in the folder, instead of looping through the attack values of all the enemies.
local EnemyFolder = workspace.Attackables
local Ambience = script.AmbienceLente
local BattleMusic = script.VengefulAttack
while true do
task.wait(0.1)
local Enemies = EnemyFolder:GetChildren()
local EnemyAttacking=false
for i, v in pairs(Enemies) do
if v:IsA("Model") and v.Attacking.Value then
EnemyAttacking=true
break
end
end
if EnemyAttacking then
if Ambience.IsPlaying() then Ambience.Stop() end
if not BattleMusic.IsPlaying() then BattleMusic.Play() end
else
if BattleMusic.IsPlaying() then BattleMusic.Stop() end
if not Ambience.IsPlaying() then Ambience.Play() end
end
end
local Ambience = script.AmbienceLente
local BattleMusic = script.VengefulAttack
while true do
task.wait(0.1)
local Enemies = EnemyFolder:GetChildren()
local EnemyAttacking=false
for i, v in pairs(Enemies) do
if v:IsA("Model") and v.Attacking.Value then
EnemyAttacking=true
break
end
end
if EnemyAttacking then
if Ambience.IsPlaying then Ambience:Stop() end
if not BattleMusic.IsPlaying then BattleMusic:Play() end
else
if BattleMusic.IsPlaying then BattleMusic:Stop() end
if not Ambience.IsPlaying then Ambience:Play() end
end
end