#broken sound in script

1 messages · Page 1 of 1 (latest)

hazy cradle
#

the sound at line 55 just starts playing and resetting over and over again, why is this? and how can i fix it?

rotund hearth
#

i think it's b/c it's acting like a while loop since it's always true
try adding a debounce to it

rotund hearth
#

or like once those variables become true then it works like a while loop thats what i meant

hazy cradle
rotund hearth
#

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

hazy cradle
#

wdym

cinder sierraBOT
#

studio** You are now Level 5! **studio

hazy cradle
#

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

fleet jay
#

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

fleet jay
#

if its something like a runservice heartbeat event, this wont work

#

you would have to implement debounce then

rotund hearth
#

something like that

#

tho this would delay the max distance and nearest target updating

fleet jay
hazy cradle
#

@fleet jay that made the sound play but the thing that is chasing me froze in place

rotund hearth
hazy cradle
#

,

fleet jay
#

@hazy cradle can you send the function in this format?

--a
#

`

hazy cradle
#

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
fleet jay
#

Okay give me a second

hazy cradle
#

alr

fleet jay
#
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

hazy cradle
#

ok

#

that works now tysm

#

:D

fleet jay
#

np

hazy cradle
# fleet jay 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'

fleet jay
#

updated it

hazy cradle
#

ok

#

tyy, it works now :D

fleet jay
#

Np

fringe forge
hazy cradle
#

Ye

fringe forge
#

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

hazy cradle
#

Its already fixed