MouseButton1Down Click Detection that Plays Animation

Basically, I am trying to make the Player interfere with the “SprintButton” which is saturated with the “RunAnimation” this only associates with Mobile users because I have added a Sprint Button and when the Player would to click the Sprint Button the Run Animation plays and when they click again the Run Animation will stop. How would I achieve it, so it works to properly extent.

What do you mean by this, saturated?

1 Like

This is the current Script I have for this System:

-- PlayerRunSystem Script
-- PC usage for StaminaSystem
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 25
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://15411322421"
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
	end
end)

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


-- Mobile usage for StaminaSystem
player:WaitForChild("PlayerGui").StaminaGui.Holder.Sprint.MouseButton1Down:Connect(function()
	local Anim = Instance.new("Animation")
	Anim.AnimationId = "rbxassetid://15411322421"
	PlayAnim = Character.Humanoid:LoadAnimation(Anim)
	PlayAnim:Play()
-- When Player clicks "SprintButton" the Animation will stop and the Players Speed will reduce
end)

Oh, basically another word for embedded.

Well your going to need a flag to tell when the player is running and then you can determine when to stop playing the animation and when to stop the player from running

1 Like

Also why are you creating a new instance of the run animation everytime they click it?

1 Like

Well, when I would to click the Sprint Button the Animation plays but when I would to click again it doesn’t stop and I don’t know how make a detection so it can comprehend to the Players interaction.

1 Like

I will send of a video of the trouble I am facing.

Well first off you should stop creating the animations as buttons are pressed, you should have the animation in some place like replicated storage and then have a variable at the top defining it and loading it, and then playing it when a player presses an input

1 Like

ezgif-5-915f92512f

I clicked the input to run the Animation and then I clicked again to stop the Animation, but as you can see it doesn’t stop.

@astraIboy do you now see my problem?

Yeah I get your problem hold on one second im fixing your script one moment

1 Like

alright thank you, very much appreciated! I’ve been working non-stop for all up to 2 weeks now!

1 Like

Also if it’s any easier here’s the Stamina System Script:

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local SoundFX = script.RunSound

local StaminaGui = script.Parent
local Holder = StaminaGui:WaitForChild("Holder")
local Bar = Holder:WaitForChild("Bar")

local UIS = game:GetService("UserInputService")

local Stamina = script.Stamina

local isRunning = false
local gotTired = false
local mobileToggle	= false

local function toggleSprint(state: boolean)
	if state and Stamina.Value >= 1 and not gotTired then
		humanoid.WalkSpeed = 25
		SoundFX:Play()

		script.InSprint.Enabled = true
		script.OutSprint.Enabled = false
	else
		humanoid.WalkSpeed = 10
		SoundFX:Stop()

		script.InSprint.Enabled = false
		script.OutSprint.Enabled = true
	end
end

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		toggleSprint(true)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		toggleSprint(false)
	end
end)

Holder.Sprint.MouseButton1Down:Connect(function()
	mobileToggle	= not mobileToggle
	
	toggleSprint(mobileToggle)
end)

script.Tired.Event:Connect(function()
	if Stamina.Value <= 1 then
		humanoid.WalkSpeed = 10
		
		script.InSprint.Enabled = false
		script.OutSprint.Enabled = true
		gotTired = true
		
		wait(1)
		
		gotTired = false
	end
end)

while true do
	wait()
	
	game:GetService("TweenService"):Create(Bar, TweenInfo.new(0.075, Enum.EasingStyle.Quint), {Size = UDim2.new(Stamina.Value / 100, 0, 1, 0)}):Play()
end

Alright try this

-- PlayerRunSystem Script
-- PC usage for StaminaSystem
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid

local anim= Instance.new("Animation")
anim.AnimationId = "rbxassetid://15411322421"
local RunAnimation =Humanoid:LoadAnimation(anim)

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 25
		RunAnimation:Play()
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 10
		RunAnimation:Stop()
	end
end)


-- Mobile usage for StaminaSystem
player:WaitForChild("PlayerGui").StaminaGui.Holder.Sprint.MouseButton1Down:Connect(function()
	if RunAnimation.IsPlaying then
      RunAnimation:Stop()
   else
      RunAnimation:Play()
   end
-- When Player clicks "SprintButton" the Animation will stop and the Players Speed will reduce
end)
1 Like

@astraIboy
I got this error associating with the Script
LoadAnimation is not a valid member of Animation "Animation" - Client - PlayerView:270

Yeah wait I updated the script copy and paste it again

1 Like

@astraIboy Thank you so much! However, is it possible to make the Animation stop playing if the speed is 0 (Player isn’t moving).

1 Like

For the mobile players or for the pc players, or both?

1 Like