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

Why Is the music not Inserting Itself to the torso even though the IntValue Is above 4?

I made a script Inside of a NPC that plays a audio when the IntValue which Is also Inside of the NPC Is In a specific point like when Its above 4, just the problem Its just not Inserting the music

Script

local Troll = script.Parent
local Killstreak = Troll.Killstreak

while wait(1) do
	wait(1)
	Killstreak.Value += 1
end

while wait() do
	if Killstreak.Value >= 4 then
		local MusicTheme = Instance.new("Sound")
		MusicTheme.Parent = Troll.Torso
		MusicTheme.Name = "Theme Song"
		MusicTheme.Volume = 3
		MusicTheme.SoundId = "rbxassets://10239133106"
		MusicTheme.Looped = true
		MusicTheme:Play()
	end
end

4 answers

Why does no one point this out?

local Troll = script.Parent
local Killstreak = Troll.Killstreak

while wait() do
    wait(1)
    Killstreak.Value += 1
    if Killstreak.Value >= 4 then
        local MusicTheme = Instance.new("Sound")
        MusicTheme.Parent = Troll.Torso
        MusicTheme.Name = "Theme Song"
        MusicTheme.Volume = 3
        MusicTheme.SoundId = "rbxassets://10239133106"
        MusicTheme.Looped = true
        MusicTheme:Play()
	-- If you want it to stop after this, use break!
    end
end

the first 'while wait(1) do' is probably preventing the code from under it working, since its in its own loop

I had to get around this myself, and coroutines were the solution. I was using repeats, however I'm somewhat sure these are the same. You can find guides about these online if it doesn't help

local co = coroutine.create(function()

while wait(1) do
		--your code
	end
end)

coroutine.resume(co)

while wait(1) do
	--your code
end

Coroutines should run both loops at the same time. If it doesn't work and you know the bottom loop is running, try using prints to find where the bad code is

Hello,

You used two While loops meaning it won't go to the other, that's why we use coroutines to not yield any of the code down below so both while loops will run!

local Troll = script.Parent
local Killstreak = Troll.Killstreak

coroutine.resume(coroutine.create(function()
	while wait(1) do
		wait(1)
		Killstreak.Value += 1
	end	
end))

while wait() do
	if Killstreak.Value >= 4 then
		local MusicTheme = Instance.new("Sound")
		MusicTheme.Parent = Troll.Torso
		MusicTheme.Name = "Theme Song"
		MusicTheme.Volume = 3
		MusicTheme.SoundId = "rbxassets://10239133106"
		MusicTheme.Looped = true
		MusicTheme:Play()
	end
end

That's because the song is constantly starting all over again:

local Troll = script.Parent
local Killstreak = Troll.Killstreak

Killstreak:GetPropertyChangedSignal("Value"):Connect(function()
	if Killstreak.Value >= 4 then
		if Troll.Torso:FindFirstChild("Theme Song") then return end

		local MusicTheme = Instance.new("Sound")
		MusicTheme.Parent = Troll.Torso
		MusicTheme.Name = "Theme Song"
		MusicTheme.Volume = 3
		MusicTheme.SoundId = "rbxassets://10239133106"
		MusicTheme.Looped = true
		MusicTheme:Play()
	end
end)

while wait(1) do
    Killstreak.Value += 1
end

And please use GetPropertChangedSignal instead of a while loop!

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