#how can you fix the sound being played over and over again while the player is the radius distance

13 messages · Page 1 of 1 (latest)

worthy sage
#

like ok i know the while true do thing on the end

#

but i want the script to ignore the player that enters the radius and check for the others

#

idk if i explain it well

waxen flume
#

what is the while true do supposed to be doing?

#

also every second is quite slow

#

if its an npc or something

worthy sage
#

yeah so i removed the while true do and i wanted to just play the sound once and not for every second

#

how do i do that?

#

nvm this is a waste

waxen flume
#

y?

worthy sage
#

ok so how do i do that

#
local bob = script.Parent -- Reference to Bob model
local detectionRange = 30 -- Detection range in studs
local soundPart = bob:FindFirstChild("Head") -- Find the part containing the sound
local sound = soundPart and soundPart:FindFirstChild("Sound") -- Find the sound instance

-- Function to check distance between Bob and players
local function checkDistance(player)
    local playerCharacter = player.Character
    local playerRootPart = playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart")
    local bobRootPart = bob:FindFirstChild("HumanoidRootPart")
    
    if playerRootPart and bobRootPart then
        local distance = (playerRootPart.Position - bobRootPart.Position).magnitude
        if distance <= detectionRange then
            return true
        end
    end
    return false
end

-- Function to play sound if a player is within detection range
local function playSoundIfPlayerClose()
    for _, player in ipairs(game.Players:GetPlayers()) do
        if checkDistance(player) then
            if sound and not sound.Playing then
                sound:Play()
            end
            break -- Play sound once for the first detected player in range
        end
    end
end

-- Check periodically if a player is within detection range
while true do
    playSoundIfPlayerClose()
    task.wait(1) -- Check every second
end
#

heres the script again