Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why Wouldn't a Sound play near the a part? [closed]

Asked by
woodengop 1134 Moderation Voter
9 years ago

I wanted to Play a sound/song on a Part, But Nothing happened, There wasn't anything in output either! Here is my code:

s=Instance.new("Sound")
player=game.Players.LocalPlayer
health=player.Character.Humanoid
while true do
    if health.Health==100 then
    s:Play()
    s.Parent=script.Parent--The Parent is a part.
    s.SoundId="rbxassetid://144260719"--asset
    s.Volume=1
    s.Looped=true
    elseif health.Health==0 then--To Stop the glitching.
        s:stop()--stops when Player is dead.
    end
end--end

Please Respect that I am a Scripter in need for knowledge

Locked by adark and Redbullusa

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
3
Answered by
yumtaste 476 Moderation Voter
9 years ago

You didn't put a wait() in the loop, you set the SoundId after you called Play() on the Sound object, and you don't need a loop if the Sound is looped. Here, I decided to rewrite your code for you, and explain what I wrote.

local player = game.Players.LocalPlayer
local sound = Instance.new("Sound", script.Parent)
local humanoid = player.Character.Humanoid
sound.SoundId = "rbxassetid://144260719"
sound.Looped = true
sound.Volume = 1
sound:Play()

humanoid.Died:connect(function() --instead of having a loop that could lag the game, this just stops the sound when the Humanoid dies
sound:Stop()
end)

Notice how I set all the properties of the Sound object before I wrote the "actual" code. I always do this so I don't have to suffer with confusion later in the script, since I'm always adding and removing parts of my scripts. If you have any questions, please comment! If this answer helps, please upvote and accept!

1
I thanks I forgot! woodengop 1134 — 9y
0
No problem! yumtaste 476 — 9y
0
Ugh! I'm failing to remember these stuff. Thx for reminding, Time to fail on my spelling Test :P woodengop 1134 — 9y
Ad