Elseif statement makes script forget variables?

I have this line of code that’s linked via RemoteEvent to a GUI Hold-button.
It tweens a sound’s pitch and volume in a specific amount of time, yet if i let go and hold again before this amount of time is up, the effect won’t be right. Thus, I am trying to somehow reset the tween once the button is let go. The problem is, the script no longer recognizes the tween variable because its after an “elseif”. Any help?

	elseif info == "Whistle On" then
		Locomotive.Whistle.Smoke.Enabled=true
		local goal = {}
		goal.PlaybackSpeed=1
		goal.Volume=3.2
		local tweenInfo = TweenInfo.new(
			.4, 
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.InOut, 
			0, 
			false, 
			0 
		)

		local tweenwhistle = TweenService:Create(Locomotive.Whistle.Sound, tweenInfo, goal)
		tweenwhistle:Play()
		Locomotive.Whistle.Sound:Play()
	elseif info == "Whistle Off" then
		--- somehow cancel/reset tween?
		Locomotive.Whistle.Smoke.Enabled=false
		Locomotive.Whistle.Sound:Stop()
		Locomotive.Whistle.Sound.PlaybackSpeed=.96
		Locomotive.Whistle.Sound.Volume=0

This is due to you using local, it will look for the Variable within the Scope, so it will simply forget the Variable when using another function, Try a Global Variable:

Variable1 = nil -- Global Variable
local Variable2 -- Local Variable

oh wow, i didn’t even know global variables were a thing, thank you!

Or just create the local variable outside of the if statement
It is discouraged to create global variables outside of the global environment

1 Like

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