I’m trying to create 2 mobile buttons for 2 different skills the player has (the only 2 I’ve made so far) No matter what I do, I can’t figure out how to use ContextActionService properly. So far, I’ve been trying to find a solution to my problem, but nothing I’ve tried is working. I watched a tutorial, and all I got from it was this part of the code to make a mobile button, and I found an old roblox blog explaining how to make mobile buttons.
local contextActionService = game:GetService("ContextActionService")
function onButtonPress()
end
local mobilebutton = contextActionService:BindAction("SprintButton", onButtonPress, true, "d")
contextActionService:SetPosition("SprintButton", UDim2.new(0.72, -25, 0.20, -25))
contextActionService:SetImage("SprintButton", "rbxassetid://11429326")
contextActionService:SetTitle("SprintButton", "Sprint")
Sprint Script:
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
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 = 27.5
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://9174155767'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 14.5
PlayAnim:Stop()
end
end)
How would I convert at least this sprint button to work on mobile? (Please explain very carefully, as I have absolutely no idea what I’m doing at all. All I just know is that this is the script on how to get a mobile button.)
1 Like
local contextActionService = game:GetService("ContextActionService")
function onButtonPress(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
Character.Humanoid.WalkSpeed = 27.5
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://9174155767'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
elseif inputState == Enum.UserInputState.End then
Character.Humanoid.WalkSpeed = 14.5
PlayAnim:Stop()
end
end
local mobilebutton = contextActionService:BindAction("SprintButton", onButtonPress, true, "d")
contextActionService:SetPosition("SprintButton", UDim2.new(0.72, -25, 0.20, -25))
contextActionService:SetImage("SprintButton", "rbxassetid://11429326")
contextActionService:SetTitle("SprintButton", "Sprint")
2 Likes
Thank you so much I’ve been on this for 3 hours and I need to sleep
1 Like
How would I be able to set this up to where the button only appears once the player has a badge?
You could use BadgeService and then bind the action.
So I have to put in the code that checks if a player has a badge and if they do have the badge the BindAction gets binded?
I added in the badge check code but now it won’t create a button
lua
local contextActionService = game:GetService("ContextActionService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
function onButtonPress(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
Character.Humanoid.WalkSpeed = 27.5
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://9174155767'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
elseif inputState == Enum.UserInputState.End then
Character.Humanoid.WalkSpeed = 14.5
PlayAnim:Stop()
end
end
local BadgeService = game:GetService("BadgeService")
local Ability = game:GetService("ReplicatedStorage"):WaitForChild("ShiftToSprint") -- Change tool to the name of your tool in replicated storage!
local BadgeId = 2125647539
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
if BadgeService:UserHasBadgeAsync(player.UserId,BadgeId) then
local mobilebutton = contextActionService:BindAction("SprintButton", onButtonPress, true, "z")
contextActionService:SetPosition("SprintButton", UDim2.new(0.72, -25, 0.20, -25))
contextActionService:SetImage("SprintButton", "rbxassetid://11429326")
contextActionService:SetTitle("SprintButton", "Sprint")
end
end)
end)
use RemoteEvent as contextaction doesnt work on server side scripts
Please clarify, idk what that is ;-;
SeverSide script is normal script, you need to use RemoteEvent then fire it if the player has the badge
For the remote event, do I connect the function with the whole mobile button script?
Could you please clarify what I’m supposed to do with the remote event?
I’ve been messing around with this script to try to fix the blue line error in the image, but nothing is working. Can you please help?