CAS If statement

I want to access the tween in the above if statement, how would I do that?

local function Slide(name, state, type)
if state == Enum.UserInputState.Begin and canslide then
	canslide = false
		local animation = Instance.new("Animation")
		animation.AnimationId = 'rbxassetid://18306255547'
		local SlideTrack = Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(animation)
		SlideTrack:Play()
			local SlideVal = Instance.new("NumberValue")
			SlideVal.Value = WalkSpeedV.Value * 2
			Humanoid.WalkSpeed = 0
			local speed = SlideVal
		local speedup = RunService.RenderStepped:Connect(function()
			SlideTrack:AdjustSpeed(speed.Value)
		end)
			local SpeedCap = RunService.Stepped:Connect(function()
				if SlideVal.Value > 25 then
					SlideVal.Value = 25
				end
			end)			
			
			
		local SlideTween = TweenService:Create(speed, TweenInfo.new(speed.Value/2), {Value = 0})
		local BV = Instance.new("BodyVelocity")
		BV.Parent = Character.PrimaryPart
		BV.MaxForce = Vector3.new(math.huge, 0, math.huge)
		SlideTween:Play()
		local VelocityStep = RunService.Stepped:Connect(function()
			 BV.Velocity = HumanoidRP.CFrame.LookVector * speed.Value
		end)
		
	SlideTween.Completed:Connect(function()
		issliding = false
		Humanoid.WalkSpeed = 15
		SlideTrack:Stop()
		BV:Destroy()
		SlideTween:Destroy()
		SlideVal:Destroy()
		animation:Destroy()
		speedup:Disconnect()
		VelocityStep:Disconnect()
		task.wait(1)
		canslide = true
	end)

end
if state == Enum.UserInputState.End then
	print("ended")
	-- End Tween, Stop Animation, Destroy Tween, Destroy BV, Destroy Animation
	end
end

I don’t think you can even tween number values.

No it’s possible cause it works, I just wanna know if it’s possible to end the tween?

You can stop the tween with SlideTween:Stop()

yeah I know, but it’s in the above If Statement, I don’t know how to access it, if even possible

Why not just initialize the tween variables in the function and outside the if statements?

2 Likes

Make a local variable on top of the function AND the if statement and make it nil so that every scope of your script can access then in your function just type the name of the variable and set it to the tween.

Example :

local example = nil

local function foo()
	example = "Hello"
end

print(example) --Prints "nil"

foo()

print(example) --Print "Hello"

I agree with this, there is no major reason as reason to why you must only define and create a new tween every time theres an input, just define them outside the function, and create them them within to:Play()

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