My if statement is not working at all for some reason.
local runS = game:GetService("RunService")
local conductor = script.Parent.Conductor
local eventFireTime = 10
function Update()
if eventFireTime <= conductor:GetAttribute("songPositionInBeat") then
print("This print statement has been fired on the tenth beat! The best part is that it is in sync!")
end
end
runS.Heartbeat:Connect(Update)
local runS = game:GetService("RunService")
local audio = game:GetService("SoundService").Background
-- Important Data
local bpm = 138.057
local secPerBeat = 0
local songPosition = 0
local songPositionInBeats = 0
local firstBeatOffset = 0
local dspSongTime = 0
function Start()
secPerBeat = 60.0 / bpm
dspSongTime = audio.TimePosition
audio:Play()
end
function Update()
songPosition = audio.TimePosition - dspSongTime - firstBeatOffset
songPositionInBeats = songPosition / secPerBeat
script:SetAttribute("songPosition", songPosition)
script:SetAttribute("songPositionInBeats", songPositionInBeats)
end
Start()
runS.Heartbeat:Connect(Update)
What the attributes are for:
for some reason it just says “0” even though it clearly goes up.
Hmmm… First, I want to remove the useless things here. The dspoffset value would always be 0 when you play or stop it.
local runS = game:GetService("RunService")
local audio = game:GetService("SoundService").Background
-- Important Data
local bpm = 138.057
local secPerBeat = 60/bpm
local songPositionInBeats = 0
local firstBeatOffset = 0
function Start()
audio:Play()
end
function Update()
songPositionInBeats = (audio.TimePosition - firstBeatOffset) / secPerBeat
script:SetAttribute("songPositionInBeats", songPositionInBeats)
end
Start()
runS.Heartbeat:Connect(Update)
Also scripts do not work in soundservice as far as I am aware.