Sound doesn't play in local script

Ok so this script doesn’t play the sound when its put in a LocalScript as I want it to be client-side, I’m pretty much a beginner so is this because it needs to be modified and put in the player folders?

local Ambience = script.Parent
local triggerdistance = 10
local debounce = false
local sound = script.Parent.Sound

local function scare()
for _, player in ipairs(game.Players:GetPlayers()) do
if player.Character then
local distance = (Ambience.Position - player.Character.HumanoidRootPart.Position).magnitude
if distance <= triggerdistance then
return true
end
end
end
return false
end

while true do
if scare() and not debounce then
debounce = true
sound:Play()
task.wait(5)
debounce = false
end
task.wait(1)
end

1 Like

Is it in workspace? LocalScripts don’t work there.

2 Likes

Yeah, its in a part in the workspace.

1 Like
local sound = Ambience:WaitForChild("Sound")

if what @ZombieCrunchUK said works then great!
also incase someone loads slowly “sound” might error “infinite yield possible on”

1 Like

Are you sure that code is right? I don’t know what I have to do to fix this.

1 Like

ok nevermind I fixed it I had to modify it slightly and put it on StarterCharacterScripts

1 Like

I believe because it uses the player.character clientside info which makes it needed to be put in PlayerCharacterScripts

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.