Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why Wouldn't a Sound play near the a part?

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

1 answer

✓ Accepted answer

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!

3 comments

1

I thanks I forgot!

0

No problem!

0

Ugh! I'm failing to remember these stuff. Thx for reminding, Time to fail on my spelling Test :P

This discussion is preserved as it was. New answers, comments, and votes are closed.