#broken sound in script
1 messages · Page 1 of 1 (latest)
i think it's b/c it's acting like a while loop since it's always true
try adding a debounce to it
its not always true
or like once those variables become true then it works like a while loop thats what i meant
ohh, so should i put the play sound somewhere else or?
add a debounce, make the sound only able tjo play if the debounce is false, and then if the other values such as distance and can see == true, make the debounce = true
then set it back to false after a certain amount of time
wdym
** You are now Level 5! **
if the debounce is set to true the sound wont play
and the sound is supposed to play at the same time as its within the distance and canseetarget
@rotund hearth
can i see what calls the findtarget function?
game.SoundService.Chase.Ended:Wait()
You can slap this line of code below the one that makes it play it if its fired from a while loop
ok
if its something like a runservice heartbeat event, this wont work
you would have to implement debounce then
create a debounce: local debounce = false
then incase the play in an if statement:
if debounce == false then
debounce = true
sound:Play()
end
--update code
task.wait()
debounce = false
something like that
tho this would delay the max distance and nearest target updating
you can just make the if statement for playing the sound
@fleet jay that made the sound play but the thing that is chasing me froze in place
yeah do a debounce
yea i did it only incases the sound
,
ye okay
local function canSeeTarget(target)
local orgin = hrp.Position
local direction = (target.HumanoidRootPart.Position - hrp.Position).Unit * 60
local ray = workspace:Raycast(orgin, direction, rayParams)
if ray and ray.Instance then
if ray.Instance:IsDescendantOf(target) then
return true
else
return false
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 60
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
local debounce = false
if distance < maxDistance and canSeeTarget(target) then
if debounce == false then
debounce = true
game.SoundService.Chase:Play()
nearestTarget = target
distance = maxDistance
end
end
end
return nearestTarget
end
Okay give me a second
alr
local SoundDb = false
local function canSeeTarget(target)
local orgin = hrp.Position
local direction = (target.HumanoidRootPart.Position - hrp.Position).Unit * 60
local ray = workspace:Raycast(orgin, direction, rayParams)
if ray and ray.Instance then
if ray.Instance:IsDescendantOf(target) then
return true
else
return false
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 60
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
if not SoundDb then
game.SoundService.Chase:Play()
SoundDb = true
end
nearestTarget = target
distance = maxDistance
else
SoundDb = false
end
end
end
return nearestTarget
end
something like that
i prob missed an end there
np
one more problem, after the sound stops playing (when it gets out of the distence or canseetarget is false) the sound wont start playing again after that'
updated it
Np
this is a npc chase script right?
Ye
more than likely it's due to the fact that you're putting the music in the calculation for moving towards a player and considering that every couple milliseconds the npc redoes the movement towards the player, it also plays the audio
causing it to play over and over and over for every redo of the movement towards the player
Its already fixed