Script won't pitch down the music when I touch a part

Hey all.

I’m having an issue trying to script this so that when you touch a part, the Octave section on the “PitchShiftSoundEffect” goes down from 1 to 0.5 smoothly. However the script is not providing me with the results I want.

Here is what I’ve got working with:

local pitch = workspace.Music.PitchShiftSoundEffect.Octave
local touchpart = script.Parent

touchpart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		
		repeat
			wait(0.01)
			pitch = pitch - 0.01
		until pitch == 0.5
		
	end
end)

Any advice is helpful and appreciated, thx!

(Also please note I’m very new to scripting so your patience is appreciated)

I don’t think you should be declaring properties in variables, since setting a variable to something else will overwrite the variable entirely.

local foo = 0
foo = 1 -- foo will equal 1 now, the original 0 will be gone!
-- MODIFIED CODE
local pitch = workspace.Music.PitchShiftSoundEffect -- Removed .Octave
local touchpart = script.Parent

local Increment = 0.01
local Goal = 0.5
-- ^^ These two variables aren't needed, but they provide control over how much you want your .Octave to change.

touchpart.Touched:Connect(function(hit: BasePart)
	if hit.Parent:FindFirstChild("Humanoid") then
		
		repeat
			task.wait(0.01) -- Use task.wait() for a better waiting time.
			pitch.Octave -= Increment -- This is the same as writing 'pitch = pitch - 0.01', it's just easier to write.
		until pitch.Octave == Goal

	end
end)

Hope this helps.

1 Like

Hmmmm, so I tried this and was met with

" Workspace.Part.Script:14: attempt to perform arithmetic (sub) on Instance and number - Server - Script:14"

I assume it has something to do with the “pitch -= Increment” line.

Whoops!

I forgot to reference .Octave.

I edited the code again, you should try that instead.

1 Like

Yup, works. Thank you so much!

1 Like