Sounds Not Playing When Using :Play(), Only Work When Playing is Set to True

I am working on an AI, and I have it set to when it detects a player, a chase sound plays. When it looses sight of a player, it stops and plays an ambient theme while it patrols. I simply just put :Play() or :Stop(), but the script ignores playing the Ambient theme, and the AI plays the chase sound, but does not chase the player. I’m very confused.

Script Section For When Player Found:

for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health>0 then --Chase
print(target.Name)
workspace.BOOM:Play()
workspace.Chase:Play()
attack(target)
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end

Script For Patroling:

local function patrol()
local waypoints = workspace.Waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
workspace.Ambient:Play()
walkTo(waypoints[randomNum].Position)

end

2 Likes

If you check the Sound’s Playing property during a Studio Play, does the box check (become blue with a :ballot_box_with_check:) when it should?

  • Perhaps it keeps spamming :Play() to the point where you don’t actually hear anything.
  • Perhaps one sound is so loud you can’t hear the other?
  • Perhaps there’s another reason behind it, since the AI doesn’t chase the player as well.

I can’t tell right now :sweat_smile: There’s also pieces of code missing as well as a codeblock (makes it easier to read).
Might want to check the first alinea or add some more ‘prints’ to detect where the issue could be

It WAS in a repeating loop I realized, so I’ve been trying to tie it into an if else then loop. Now I’m just confused on how to check if it’s playing or not because apparently you can’t use .IsPlaying.

Mhm… I have it a go as well.


https://developer.roblox.com/en-us/api-reference/property/Sound/IsPlaying

And indeed, you can’t set that value, just as the article above explains.

script.Parent["Sun Massacre Instru"].Playing = false

.IsPlaying Playing could be changed however.

.IsPlaying can be read from, so I assume you could check it via an if statement. Using your own variable (local AmbientIsPlaying = false) might work too :thinking:

1 Like

I tried putting a bool value that is true or false if the player is being chased, and it seems to be working better? Now the script is just not playing them though.

Less messy, but also here is the code for the value:

local Sounds = workspace.VERYIMPORTANTPARTNODELETE
local chased = script.Parent
Sounds.Ambient:Play()
while wait(.5) do
if chased.Value == true then
Sounds.BOOM:Play()
Sounds.Chase:Play()
Sounds.Ambient:Stop()
elseif chased.Value == false then
Sounds.BOOM:Stop()
Sounds.Chase:Stop()
Sounds.Ambient:Play()
end
end

code.txt (340 Bytes)