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

Making sound tone down in volume after time script [isn't working?]

local time = game.Lighting.TimeOfDay
local t = tonumber(string.sub(time,1,2)..string.sub(time,4,5)..string.sub(time,7,8)) --convert time to a number

if t >= 73000 then
	for i = .2,0,-.001 do --loop that turns music down over 2 seconds
		wait(.01)
		script.Sunset.Volume = i
			if i == 0 then
				script.Sunset:Stop()
				break --is this necessary?
			end
	end 
	
script.Dawn:Play()

elseif t >= 200000 then
	for i = .2,0,-.001 do --same for this  ! 
		wait(.01)
		script.Dawn.Volume = i
			if i == 0 then
				script.Dawn:Stop()
				break
			end
	end
	
script.Sunset:Play()

end

I tried this on a multiplayer server, and the song for Dawn wouldn't tone down to 0 volume when the time came along. Any ideas?

ps: Huge thanks to the other guy that suggested I turned the strings into numbers so I could compare them using tonumber.

1 comment

0

You're welcome! By the way, wait() has a minimum argument: 0.03. Anything below that is the same as wait().

1 answer

While I'm trying to figure it out, here's what I made from scratch.

local time = game.Lighting.TimeOfDay
local t = tonumber(string.sub(time,1,2)..string.sub(time,4,5)..string.sub(time,7,8)) --convert time to a number

if t >= 73000 and t < 200000 then
for i = .2,0,-.001 do
wait()
script.Sunset.Volume = i
end
script.Sunset:Stop()
script.Sunset.Volume = .2
script.Dawn:Play()

elseif t >= 200000 then
for i = .2,0,-.001 do
wait()
script.Dawn.Volume = i
end
script.Dawn:Stop()
script.Dawn.Volume = .2
script.Sunset:Play()
end

6 comments

1

You should tab your scripts, It makes it easier to read. If you say it's unnecessary, I got that tip from BlueTaslem.

0

It's unnecessary :P

0

I didn't even think about changing the volume back to .2, Thanks again. Also, I'm working on it now :P

0

I think that's more 'common-sense' rather than a tip, Lord...

0

Seems my code works for me. Does it give you any error? are you sure the code is even running? is it looped?

0

One problem you might come across is that when the code first runs: it expects a sound to be playing. that means if the first if statement is true, it will loop without playing a sound but rather just manipulating the volume, and then it will play the dawn sound. I suggest you start the game with one sound already playing and the time set at an appropriately in reference to the song.

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