Issues code functions and input call

Hello! Could help me with my code? Please…
I need to make when pressing shift, the function ‘RunAnimCall’ is called, how do I make it possible?

How could i call this:

local function OnRun()
	UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
		if input.KeyCode == Enum.KeyCode.LeftShift then
			Character.Humanoid.WalkSpeed = 35
			local Anim = Instance.new('Animation')
			Anim.AnimationId = 'rbxassetid://913376220'
			PlayAnim = Character.Humanoid:LoadAnimation(Anim)
			PlayAnim:Play()
		end
	end)
end

local function OnStop()
	UIS.InputEnded:connect(function(input)
		if input.KeyCode == Enum.KeyCode.LeftShift then
			Character.Humanoid.WalkSpeed = 16
			PlayAnim:Stop()
		end
	end)
end

In this code:

local rcall = true

local function RunAnimCall()
	if rcall == true then
		OnRun()
		script.IsRun:FireServer()
	elseif rcall == false then
		OnStop()
		script.NoRun:FireServer()
	else
		print("Nothing happens.")
	end
end

Could you just combine the scripts, then you can call the RunAnimCall from another function?

mm i am not sure how to do that, could you give me a example please :frowning: am sorry about that

Something like this? and how i could call this to a function?

UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local function OnPressed()
			Character.Humanoid.WalkSpeed = 35
			local Anim = Instance.new('Animation')
			Anim.AnimationId = 'rbxassetid://913376220'
			PlayAnim = Character.Humanoid:LoadAnimation(Anim)
			PlayAnim:Play()
			UIS.InputEnded:connect(function(input)
				if input.KeyCode == Enum.KeyCode.LeftShift then
					Character.Humanoid.WalkSpeed = 16
					PlayAnim:Stop()
				end
			end)
		end -- function OnPressed	
	end
end)

You’re making a connection everytime the player presses LeftShift which is bad, you should instead separate them, like so:

UIS.InputBegan:Connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 35
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://913376220'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16
		PlayAnim:Stop()
	end
end)
1 Like

How could you call these inputs in this function?

local rcall = true

local function RunAnimCall()
	if rcall == true then
		
	else
		
	end
end

I don’t really know what you mean, but if you want everytime you start sprinting or stop sprinting to execute that function you can simply do this:

function RunAnimCall(rcall)
	if rcall then -- If started sprinting
		print("Started sprinting")
	else -- Stopped sprinting
		print("Stopped sprinting")
	end
end

UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 35
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://913376220'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		RunAnimCall(true) -- Calls RunAnimCall with rcall as true.
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16
		PlayAnim:Stop()
		RunAnimCall(false) -- Calls RunAnimCall with rcall as false.
	end
end)

Everytime you start sprinting rcall will be true, everytime you stop sprinting rcall will be false.

1 Like

Oh thanks you! I think I was doing things wrong, I got very confused, thanks for helping me :smiley:

2 Likes