0

# 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


1
Edited 7 days ago

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

0
Edited 7 days ago

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
end
end)

coroutine.resume(co)

while wait(1) do
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

0
MattVSNNL 614
7 days ago

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

0

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