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

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

Asked by 1 year ago

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

Log in to vote
1
Answered by
ryanzhzh 128
1 year ago
Edited 1 year 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
Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year 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
        --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

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
1 year 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
Log in to vote
0
Answered by 1 year ago

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!

Answer this question