Hello , I have a script that will generate a line of ‘coins’. each coin has a sound effect in it. So if you’ve ever played subway surfers, yo will know that if you collect multiple coins in a row, the sound effect gets faster each coin. I want to have that same effect, but I have no idea where to start. I tried finding the closeest part, but it would not work. Can i have help please
You could create a localscript which is responsible for playing the sound.
When your character touches a coin, you can keep track of two variables
lastCoinTouchTime
- The time the character touched the last coin.
coinTouchCount
- How many coins have been touched so far.
You could check if the lastCoinTouchTime
is within a threshold (e.g. 5 seconds), you can increment coinTouchCount
and set the Sound’s pitch to it. Otherwise set coinTouchCount
to 0.
Pseudo-code:
-- Localscript
local lastCoinTouchTime = 0
local coinTouchCount = 0
function coinTouched()
if (os.clock() - lastCoinTouchTime) < 5 then
coinTouchCount += 1
sound.PlaybackSpeed = 0.8 + 0.2 * coinTouchCount
else
coinTouchCount = 0
sound.Pitch = 0
end
lastCoinTouchTime = os.clock()
sound:Play()
end
So like @majdTRM has said, you would save the time of the last time you collected a coin, in a variable. Use this to determine if you’re still on a streak of collecting coins.
This is with simple logic:
local lastcointouch = 0
local coincombo = 0
local pitchloop = {1.1,1.13,1.15,1.13}
local function OnCoinTouch()
if tick() - lastcointouch < 1 then
coincombo = coincombo + 1
end
-- Formula and logic should be tinkered with so you get a pitch you like!
local Combo = coincombo
local pitch = 1 + (Combo / 100)
if pitch > 1.15 then pitch = 1.15 end
sound.PlaybackSpeed = pitch
sound:Play()
lastcointouch = tick()
-- rest coin script here
end
This is with better sounding, more complex logic.
You should tinker with the numbers to get a sound you like.
Hopefully it works a little:
local lastcointouch = 0
local coincombo = 0
local pitchloop = {1.1,1.13,1.15,1.13}
local function OnCoinTouch()
if tick() - lastcointouch < 1 then
coincombo = coincombo + 1
end
-- Formula and logic should be tinkered with so you get a pitch you like!
local Combo = coincombo
local pitch = 1
if Combo >= 15 then
Combo = Combo - 15
Combo = Combo % 4
pitch = pitchloop[Combo + 1]
else
pitch = 1 + (Combo / 100)
end
sound.PlaybackSpeed = pitch
sound:Play()
lastcointouch = tick()
-- rest coin script here
end
You pasted the 3 scripts 3 times, could you fix that and also use the code block feature?
print("Hello world!")