Why InputEnded doesnt cancel InputBegan?

local player = game.Players.LocalPlayer

remote = script:WaitForChild("RokushikiRemote")

UIS = game:GetService("UserInputService")

debounce = false

local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("fireball")

local TweenService = game:GetService("TweenService")

UIS.InputBegan:Connect(function(Input, isTyping)

if not isTyping then

elseif Input.KeyCode == Enum.KeyCode.R and not debounce then
			debounce = true

			local character = player.Character

			local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("Fireball"):Clone()
			fireball.Parent = game.Workspace
			fireball.Size = Vector3.new(2, 2, 2)
			fireball.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 10, 0)
			fireball.Massless = true


			local Info = TweenInfo.new(3, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, false, 0)
			local properties = {
				Color = Color3.fromRGB(255, 0, 0),
				Size = Vector3.new(15, 15, 15),
				Anchored = true
			}
			local Tween = TweenService:Create(fireball, Info, properties)
			Tween:Play()
			Tween.Completed:Wait()


			wait()
			debounce = false

		end

	end
end)
UIS.InputEnded:Connect(function(Input, isTyping)
	
	if not isTyping then
		
		if Input.KeyCode == Enum.KeyCode.R and not debounce then
			
			fireball.Anchored = false	
			local bv = Instance.new("BodyVelocity", fireball)
			bv.Velocity = CFrame.new(fireball.CFrame.Position,player:GetMouse().Hit.p).lookVector * 50
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			
		end
	end
	
	
	
	
end)

What I meant to do here is that to end the tweenservice, when I unpress R. But it wont work and it wont even give body velocity to the fireball, I tried putting Tween:Destroy() it only puts grammar error on Tween. How can I cancel the tween in inputbegan? so that I when I unpress it, it will give bv to fireball and move

The reason for this is because the Tween variable only exists in the scope of the InputBegan function, so what you need to do is change your code to this:

local player = game.Players.LocalPlayer

remote = script:WaitForChild("RokushikiRemote")

UIS = game:GetService("UserInputService")

debounce = false

Tween = nil

local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("fireball")

local TweenService = game:GetService("TweenService")

UIS.InputBegan:Connect(function(Input, isTyping)

if not isTyping then

elseif Input.KeyCode == Enum.KeyCode.R and not debounce then
			debounce = true

			local character = player.Character

			local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("Fireball"):Clone()
			fireball.Parent = game.Workspace
			fireball.Size = Vector3.new(2, 2, 2)
			fireball.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 10, 0)
			fireball.Massless = true


			local Info = TweenInfo.new(3, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, false, 0)
			local properties = {
				Color = Color3.fromRGB(255, 0, 0),
				Size = Vector3.new(15, 15, 15),
				Anchored = true
			}
			Tween = TweenService:Create(fireball, Info, properties)
			Tween:Play()
			Tween.Completed:Wait()


			wait()
			debounce = false

		end

	end
end)
UIS.InputEnded:Connect(function(Input, isTyping)
	
	if not isTyping then
		
		if Input.KeyCode == Enum.KeyCode.R and not debounce then
			
			if Tween then
				Tween:Cancel()
			end

			fireball.Anchored = false	
			local bv = Instance.new("BodyVelocity", fireball)
			bv.Velocity = CFrame.new(fireball.CFrame.Position,player:GetMouse().Hit.p).lookVector * 50
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			
		end
	end
	
	
	
	
end)

I didn’t test this code yet, but hopefully it works.

There is still a red line under the tween, I dont know how to send arguments to it

Try this?

local player = game.Players.LocalPlayer
local remote = script:WaitForChild("RokushikiRemote")
local UIS = game:GetService("UserInputService")
local debounce = false
local Tween = nil
local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("fireball")
local TweenService = game:GetService("TweenService")

UIS.InputBegan:Connect(function(Input, isTyping)

	if not isTyping then
		if Input.KeyCode == Enum.KeyCode.R and not debounce then
			debounce = true

			local character = player.Character

			local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("Fireball"):Clone()
			fireball.Parent = game.Workspace
			fireball.Size = Vector3.new(2, 2, 2)
			fireball.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 10, 0)
			fireball.Massless = true


			local Info = TweenInfo.new(3, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, false, 0)
			local properties = {
				Color = Color3.fromRGB(255, 0, 0),
				Size = Vector3.new(15, 15, 15),
				Anchored = true
			}
			Tween = TweenService:Create(fireball, Info, properties)
			Tween:Play()
			Tween.Completed:Wait()


			wait()
			debounce = false

		end

	end
end)

UIS.InputEnded:Connect(function(Input, isTyping)
	
	if not isTyping then
		
		if Input.KeyCode == Enum.KeyCode.R and debounce then
			
			if Tween then
				Tween:Cancel()
			end

			fireball.Anchored = false	
			local bv = Instance.new("BodyVelocity", fireball)
			bv.Velocity = CFrame.new(fireball.CFrame.Position,player:GetMouse().Hit.p).lookVector * 50
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			
		end
	end	
end)

The only issue I saw that was related to what’s happening with you is that in the InputEnded event, you were checking if R was ended and debounce is false. The fact that you were checking if it was false was the main issue as whilst the tween is playing, it’s set to true, meaning it couldn’t stop the tween and make the BodyVelocity until the tween was finished

It now works thanks a lot.

dadadasadad;

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like
local player = game.Players.LocalPlayer
local remote = script:WaitForChild("RokushikiRemote")
local UIS = game:GetService("UserInputService")
local debounce = false
local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("fireball")
local TweenService = game:GetService("TweenService")

UIS.InputBegan:Connect(function(Input, isTyping)
if Input.KeyCode == Enum.KeyCode.R and not debounce then
			debounce = true
			
			
			local character = player.Character
			 fireball = fireballA:Clone()
			local hum = character:FindFirstChild("Humanoid")
			hum.WalkSpeed = 0
			hum.JumpPower = 0
			
			fireball.Parent = game.Workspace
			fireball.Size = Vector3.new(2, 2, 2)
			fireball.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 10, 0)
			fireball.Massless = true
			fireball.Anchored = true


			local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
			local properties = {
				Color = Color3.fromRGB(255, 0, 0),
				Size = Vector3.new(15, 15, 15),
				
			}
			Tween = TweenService:Create(fireball, Info, properties)
			Tween:Play()
			
			

			wait()
			debounce = false


		end

	end
end)


UIS.InputEnded:Connect(function(Input, isTyping)
	
	if not isTyping then
		
		if Input.KeyCode == Enum.KeyCode.R and debounce then
			
			if Tween then
				Tween:Cancel()
				
			elseif Tween.Completed then
				
				Tween.Completed:Connect(function()
					local character = player.Character
					local hum = character:FindFirstChild("Humanoid")


					fireball.Anchored = false	
					local bv = Instance.new("BodyVelocity", fireball)
					bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
					bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					hum.WalkSpeed = 16
					hum.JumpPower = 50
				end) 
			end
				
			local character = player.Character
			local hum = character:FindFirstChild("Humanoid")


			fireball.Anchored = false	
			local bv = Instance.new("BodyVelocity", fireball)
			bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			
			hum.WalkSpeed = 16
			hum.JumpPower = 50
			
		end
	end

There was a bug when the tween completed it give bv to the fireball, so I made an if statement but it still wont work as it will only read the inputbegan.

if Input.KeyCode == Enum.KeyCode.R and not debounce then
			debounce = true
			
			
			local character = player.Character
			 fireball = fireballA:Clone()
			local hum = character:FindFirstChild("Humanoid")
			hum.WalkSpeed = 0
			hum.JumpPower = 0
			
			fireball.Parent = game.Workspace
			fireball.Size = Vector3.new(2, 2, 2)
			fireball.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 10, 0)
			fireball.Massless = true
			fireball.Anchored = true


			local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
			local properties = {
				Color = Color3.fromRGB(255, 0, 0),
				Size = Vector3.new(15, 15, 15),
				
			}
			Tween = TweenService:Create(fireball, Info, properties)
			Tween:Play()
			Tween.Completed:Connect(function()
				local character = player.Character
				local hum = character:FindFirstChild("Humanoid")


				fireball.Anchored = false	
				local bv = Instance.new("BodyVelocity", fireball)
				bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
				bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

				hum.WalkSpeed = 16
				hum.JumpPower = 50
			end) 
			

			wait()
			debounce = false

			
		end

	end
end)

but if I put it like this the inputended will still wont be read but it will the fireball can be thrown, how do I use an if-statement here that if it completes the tween it will read the function, elseif not it will be canceled by the input ended. Thanks for the help!

Wait what d oyou mean, could you explain what you want again

https://gyazo.com/fc760d3870ac43b90c5f669a55b41455

This is what happens in the second script, though it gives motion to fireball I wont be able to cancel the tween using inputended even when I unpress it. It will automatically completes the tween and fires it.

In the 1st code: I made an elseif statement below the

if Tween then
Tween:Cancel()
end

and this will happen

https://gyazo.com/3166a10ac4ef1d5d09a721403b887fac

How can I make an if statement that if the tween is completed it will run this function.

elseif Tween.Completed then
				
				Tween.Completed:Connect(function()
					local character = player.Character
					local hum = character:FindFirstChild("Humanoid")


					fireball.Anchored = false	
					local bv = Instance.new("BodyVelocity", fireball)
					bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
					bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					hum.WalkSpeed = 16
					hum.JumpPower = 50
				end) 
			end

Here you wrote it so that if the tween is completed, and then it check again if the tween is completed via the event, I don’t think you need that

elseif Tween.Completed then
				
					local character = player.Character
					local hum = character:FindFirstChild("Humanoid")


					fireball.Anchored = false	
					local bv = Instance.new("BodyVelocity", fireball)
					bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
					bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					hum.WalkSpeed = 16
					hum.JumpPower = 50
			end

Try this

Edit: Also in the InputBegan, it’ll immediately set debounce back to false, I think it’s best you set debounce to false at the end of the InputEnded for R

1 Like

It will still hang in the air, ill try using for loop this time.

Thank you for the help tho <3

1 Like

Anytime, I’ll always be here to help if you have anymore issues

1 Like
Tween.Completed:Connect(function(playbackState)
				if playbackState == Enum.PlaybackState.Completed then
					local character = player.Character
					local hum = character:FindFirstChild("Humanoid")


					fireball.Anchored = false	
					local bv = Instance.new("BodyVelocity", fireball)
					bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
					bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					hum.WalkSpeed = 16
					hum.JumpPower = 50
				elseif playbackState == Enum.PlaybackState.Cancelled then
					local character = player.Character
					local hum = character:FindFirstChild("Humanoid")


					fireball.Anchored = false	
					local bv = Instance.new("BodyVelocity", fireball)
					bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
					bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					hum.WalkSpeed = 16
					hum.JumpPower = 50
				end
			end)

I saw something in the roblox website, is this code correct? it still does the same thing with the other one but can I use this to make the if-statement in the tween, if its completed or cancelled.

It looks like it would work, although the only thing I find weird is that you did 2 statements for the same code, can’t you put that in the same statement?

Tween.Completed:Connect(function(playbackState)
				if playbackState == Enum.PlaybackState.Completed or playbackState == Enum.PlaybackState.Cancelled then
					local character = player.Character
					local hum = character:FindFirstChild("Humanoid")


					fireball.Anchored = false	
					local bv = Instance.new("BodyVelocity", fireball)
					bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
					bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					hum.WalkSpeed = 16
					hum.JumpPower = 50
				end
			end)
1 Like

Ya I forgot they were the same, but it still will complete the tween and InputEnded wont be read.

I think InputEnded wont be read because of debounce = false in the InputBegan section, maybe set debounce to false at the end of InputBegan so InputEnded as a chance to work?

If that doesn’t work, may I see your current code?

local player = game.Players.LocalPlayer
remote = script:WaitForChild("RokushikiRemote")
UIS = game:GetService("UserInputService")
debounce = false
local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("fireball")
local TweenService = game:GetService("TweenService")
local fireballA = game:GetService("ReplicatedStorage"):FindFirstChild("Fireball")
	UIS.InputBegan:Connect(function(Input, isTyping)

		if not isTyping then
if Input.KeyCode == Enum.KeyCode.R and not debounce then
			debounce = true
			
			
			local character = player.Character
			 fireball = fireballA:Clone()
			local hum = character:FindFirstChild("Humanoid")
			hum.WalkSpeed = 0
			hum.JumpPower = 0
			
			fireball.Parent = game.Workspace
			fireball.Size = Vector3.new(2, 2, 2)
			fireball.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 10, 0)
			fireball.Massless = true
			fireball.Anchored = true


			local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
			local properties = {
				Color = Color3.fromRGB(255, 0, 0),
				Size = Vector3.new(15, 15, 15),
				
			}
			Tween = TweenService:Create(fireball, Info, properties)
			Tween:Play()
			
			Tween.Completed:Connect(function(playbackState)
					if playbackState == Enum.PlaybackState.Completed or playbackState == Enum.PlaybackState.Cancelled then
						local character = player.Character
						local hum = character:FindFirstChild("Humanoid")


						fireball.Anchored = false	
						local bv = Instance.new("BodyVelocity", fireball)
						bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
						bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

						hum.WalkSpeed = 16
						hum.JumpPower = 50

					end
				end)
		
			wait()
			debounce = false

			
		end

	end
end)


UIS.InputEnded:Connect(function(Input, isTyping)
	
	if not isTyping then
		
		if Input.KeyCode == Enum.KeyCode.R and debounce then
			
			if Tween then
				Tween:Cancel()
			end
		
			local character = player.Character
			local hum = character:FindFirstChild("Humanoid")


			fireball.Anchored = false	
			local bv = Instance.new("BodyVelocity", fireball)
			bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

			hum.WalkSpeed = 16
			hum.JumpPower = 50
			
		end
	end
	
	
	
	
end)

This is the new one, do I put the debounce = false inside the tween.Completed:Connect()?

Are you trying to make it so when you release R OR when the tween is completed it fires it? If so, then I think yea. Try changing your code to this

local player = game.Players.LocalPlayer
remote = script:WaitForChild("RokushikiRemote")
UIS = game:GetService("UserInputService")
debounce = false
local fireball = game:GetService("ReplicatedStorage"):FindFirstChild("fireball")
local TweenService = game:GetService("TweenService")
local fireballA = game:GetService("ReplicatedStorage"):FindFirstChild("Fireball")
	UIS.InputBegan:Connect(function(Input, isTyping)

		if not isTyping then
if Input.KeyCode == Enum.KeyCode.R and not debounce then
			debounce = true
			
			
			local character = player.Character
			 fireball = fireballA:Clone()
			local hum = character:FindFirstChild("Humanoid")
			hum.WalkSpeed = 0
			hum.JumpPower = 0
			
			fireball.Parent = game.Workspace
			fireball.Size = Vector3.new(2, 2, 2)
			fireball.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 10, 0)
			fireball.Massless = true
			fireball.Anchored = true


			local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
			local properties = {
				Color = Color3.fromRGB(255, 0, 0),
				Size = Vector3.new(15, 15, 15),
				
			}
			Tween = TweenService:Create(fireball, Info, properties)
			Tween:Play()
			
			Tween.Completed:Connect(function(playbackState)
				if playbackState == Enum.PlaybackState.Completed or playbackState == Enum.PlaybackState.Cancelled then
					local character = player.Character
					local hum = character:FindFirstChild("Humanoid")


					fireball.Anchored = false	
					local bv = Instance.new("BodyVelocity", fireball)
					bv.Velocity = CFrame.new(fireball.CFrame.Position, player:GetMouse().Hit.p).lookVector * 50
					bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					hum.WalkSpeed = 16
					hum.JumpPower = 50

					debounce = false
				end
			end)			
		end

	end
end)


UIS.InputEnded:Connect(function(Input, isTyping)
	
	if not isTyping then
		
		if Input.KeyCode == Enum.KeyCode.R and debounce then
			
			if Tween then
				Tween:Cancel()
			end
			
		end
	end
end)

I removed the code for the fireball from InputEnded because you’re cancelling the the Tween anyways, which will run the code in Tween.Completed, and there there is the line that sets debounce to false. Try it out and tell me if you run into any issues

1 Like

Thank you for being patient and answering the question, this is my first time scripting. God Bless <3

1 Like

Anytime! That’s what I am here for, to help as much as I can! If you have anymore issues don’t be afraid to make another post!

1 Like