Help with linking mobile button to stamina

Hello

  1. What do you want to achieve? I would like a linked mobile button to the stamina

  2. What is the issue? I dont know how to link them

  3. What solutions have you tried so far? I looked for tutorials, I couldnt find any good ones

--Buttonscript
local contextActionService = game:GetService("ContextActionService")
local playerName = script.Parent.Parent.Name
local sprintValue = Instance.new("BoolValue")
sprintValue.Parent = script
local sprint = sprintValue.Value
function onButtonPress()
	
	if sprint == true then
		sprint = false
		--game.Workspace[""..playerName..""].Humanoid.WalkSpeed = 14
	else
		sprint = true
		--game.Workspace[""..playerName..""].Humanoid.WalkSpeed = 28
	end
	
end
local mobilebutton = contextActionService:BindAction("SprintButton",onButtonPress,true,"Sprint")
contextActionService:SetPosition("SprintButton",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("SprintButton","rbxassetid://2572666627")

-- Staminascript

local UIS = game:GetService('UserInputService')

local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local player = game.Players.LocalPlayer

local NormalWalkSpeed = 16
local NewWalkSpeed = 30

local power = 10

local sprinting = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character

--= FunçÔes =--

UIS.InputBegan:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.Q and gameProcessed == false then
		character.Humanoid.WalkSpeed = NewWalkSpeed
		sprinting = true
		while power > 0 and sprinting do
			power = power - .1
			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)

UIS.InputEnded:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.Q and gameProcessed == false then
		character.Humanoid.WalkSpeed = NormalWalkSpeed
		sprinting = false
		while power < 10 and not sprinting do
			power = power + .03
			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
			--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)



Anything would help, except for something unhelpful

Thanks for reading

1 Like

You can make the functions also connect to input to the button and add or key.UserInputType == Enum.UserInputType.MouseButton1.

Why don’t you just use ContextAction?

function onButtonPress(inputName, inputState)
    if inputState == Enum.UserInputState.Begin then
        -- your stamina code while the player is sprinting
    elseif inputState == Enum.UserInputState.End then
        -- your stamina code while the player is not sprinting
    end
end

Okay, I put this in the mobile script and got an error

Infinite yield possible on ‘Players.Coolsbloxian.PlayerGui:WaitForChild(“Background”)’

and no button is showing on the mobile screen

local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local player = game.Players.LocalPlayer

local NormalWalkSpeed = 16
local NewWalkSpeed = 30

local power = 10

local sprinting = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character



local contextActionService = game:GetService("ContextActionService")
local playerName = script.Parent.Parent.Name
local sprintValue = Instance.new("BoolValue")
sprintValue.Parent = script
local sprint = sprintValue.Value
function onButtonPress()
	
	function onButtonPress(inputName, inputState)
		if inputState == Enum.UserInputState.Begin then
			character.Humanoid.WalkSpeed = NewWalkSpeed
			sprinting = true
			while power > 0 and sprinting do
				power = power - .1
				Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
				Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
				wait()
				if power <= 0 then
					character.Humanoid.WalkSpeed = NormalWalkSpeed
					
				elseif inputState == Enum.UserInputState.End then
					character.Humanoid.WalkSpeed = NormalWalkSpeed
					sprinting = false
					while power < 10 and not sprinting do
						power = power + .03
						Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
						Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
						wait()
						if power <= 0 then
							character.Humanoid.WalkSpeed = NormalWalkSpeed
		end
	end
end
end
local mobilebutton = contextActionService:BindAction("SprintButton",onButtonPress,true,"Sprint")
contextActionService:SetPosition("SprintButton",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("SprintButton","rbxassetid://2572666627")

		end
	end
	end

I think you deleted the background GUI.

The frame is still there

Or do you mean code?

Okay, I don’t mean in the code. Does the script’s parent have something named “Background” inside?

I will have to check tomorrow, I am on mobile rn
Thanks for replying :+1:

Yes it does, and it still doesnt work for some reason