Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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
			}
			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

2 answers

✓ Accepted answer

In times where I need a variable in two different scopes, I create an empty variable outside both of them (or create a global variable)

Like so:

local Tween, fireball
UserInputService.InputBegan:Connect(function()
	fireball = ServerStorage.Fireball:Clone()	
	Tween = TweenService:Create(a,b,c)
	Tween:Play()
end)
UserInputService.InputEnded:Connect(function()
	fireball:Destroy()
	Tween:Destroy()
end)

What you're doing is creating a local variable inside InputBegan and trying to access it from InputEnded. Local variables only exist in the scope that they are used which is why you can't get fireball or tween in InputEnded.

Furthermore, you have fireball defined twice. On lines 09 and 22.

3 comments

0

what to do if tween completed?

0

I cant put the codes in the comments I used the answer can you help me on how to make an if statement that if the tween is completed it will run a function

0

how do I use playbackstate?, does it only works on completed tween?

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()
			
			if 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
			

			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

I got help from dev forum, this is the new code but the problem is that in the if the tween is completed it the fireball part will just float in the air.

2 comments

0

You can connect an event to a tween; tween.Completed:Connect().

0

I did put this in InputBegan, and now it will wait for the tween to be completed. The InputEnded will not be read anymore

This discussion is preserved as it was. New answers, comments, and votes are closed.