Pitch Randomization Does Not Function

i have a script for a typewriter effect. it functions. great. but, when i try to randomize the pitch of the sound, it just does not function whatsoever.

what have i tried so far?
i’ve tried using math.random to set the playback speed after i clone it, but before i play it.

this is my script as of right now.
local textTable = {
	"this is a simple typewriter effect. blah blah blah, yeah yeah. text.",
	"sometimes, i dream about cheese.",
	"charging up my typewriter!!!",
	"i-i-i love robux gift cards",
	"this is for: debugging, and testing punctuation.",
	"i just got here; the traffic was terrible. now, consider the following: AAAAGGHH! JEEEJJGH?!?! ok. that's all.",
	"for every person who dreams of the electric lightbulb, there's the one who dreams of the atom bomb.",
	"me personally, i wouldn't let that one slide",
	"mild headache??"
}

local textLabel = script.Parent:WaitForChild("Frame").TextLabel
local soundService = game:GetService("SoundService")
-- local runService = game:GetService("RunService")

local configuration = {
	["playSound"] = true,
	["speechSound"] = false, -- i should make it so that sounds can stack. that would be cool
	["typewriterSound"] = true, -- convert into a module afterwards
	["sfxAtBeginningOfText"] = false
}

local maxLength = 0
for _, text in ipairs(textTable) do
	maxLength = math.max(maxLength, #text)
end

local sentencePauseCharacters = { ",", ":", ";", "-" }
local sentenceEndingCharacters = { ".", "?", "!" }

local fixedDuration = 3
local pauseDuration = 0.3 -- how long are the pauses?
local isPaused = false

local function autoText(object, text)
	object.MaxVisibleGraphemes = 0
	object.Text = text
	local duration = fixedDuration * (#text / maxLength)

	for i = 1, #text do
		if isPaused then
			repeat task.wait() until not isPaused
		end

		local char = text:sub(i, i)
		if char ~= " " then
			if table.find(sentencePauseCharacters, char) then
				coroutine.wrap(function()
					isPaused = true
					task.wait(pauseDuration)
					isPaused = false
				end)()
			elseif table.find(sentenceEndingCharacters, char) then
				coroutine.wrap(function()
					isPaused = true
					task.wait(pauseDuration * 1.75)
					isPaused = false
				end)()
			end
		end
		object.MaxVisibleGraphemes = i
		task.wait(duration / #text)

		local isSoundPlaying = false

		if configuration.playSound and configuration.typewriterSound and not isSoundPlaying then
			local typewriterSFX = script.debug_typewriite:Clone()
			typewriterSFX.PlaybackSpeed = math.random(0.8, 1.1) -- this does not function
			isSoundPlaying = true
			soundService:PlayLocalSound(typewriterSFX)
			task.wait(typewriterSFX.TimeLength + 0.03)
			game:GetService("Debris"):AddItem(typewriterSFX, 0.01)
			isSoundPlaying = false
		end
	end
end

while task.wait(5) do
	autoText(textLabel, textTable[math.random(1, #textTable)])
end
2 Likes

i figured it out. instead of trying to hard-code the random pitch, i had to use a little bit of a workaround.

typewriterSFX.PlaybackSpeed = 0.8 + math.random() * 0.3 -- random number between 0.8 and 1.1
4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.