How can i implement this holding skill into mobile?

I got help making a script where when a keybind is held, a beam is fired but im having trouble implemting it into mobile

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local c = plr.Character
local mouse = plr:GetMouse()
local func = game:GetService("ReplicatedStorage").Remotes.KameFunc
local rs = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local kamehamecharge = game:GetService("ReplicatedStorage").Animations.KamehamehaCharge
local kamehamefire = game:GetService("ReplicatedStorage").Animations.KamehamehaFire
local chargetrack = animator:LoadAnimation(kamehamecharge)
local firetrack = animator:LoadAnimation(kamehamefire)
local remotes = game:GetService("ReplicatedStorage"):FindFirstChild("Remotes")
local playergui = plr:FindFirstChild("PlayerGui")
local mobilebuttons = playergui:WaitForChild("Buttons")
local deleteab = remotes:FindFirstChild("DeleteAbility")


local hrp, hum = c:WaitForChild("HumanoidRootPart"), c:WaitForChild("Humanoid")
local Holding = false
local Dead = Enum.HumanoidStateType.Dead

local function FaceMouse()

	local bp = Instance.new("BodyPosition")
	bp.MaxForce = Vector3.new(900000, 900000, 900000)
	bp.P = 7000
	bp.D = 500
	bp.Position = hrp.Position
	bp.Parent = hrp

	local bg = Instance.new("BodyGyro")
	bg.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
	bg.P = 9000
	bg.D = 500
	bg.CFrame = CFrame.new(hrp.Position, CFrame.new(mouse.hit.Position.X, hrp.Position.Y, mouse.hit.Position.Z).Position)
	bg.Parent = hrp

	return bp, bg
end

local function IgnoreList()
	local IG = {workspace.FX}
	for i,v in pairs(game.Players:GetChildren()) do pcall(function() 
			table.insert(IG, v.Character) 
		end)
	end

	return IG
end

local function ButtonDown(input, typing)
	if typing or hum:GetState() == Dead then return end

	if input.KeyCode == Enum.KeyCode[script:GetAttribute("KeyCode")] and script:GetAttribute("CanAttack") then

		script:SetAttribute("CanAttack", false)
		Holding = true
		chargetrack.Looped = true

		local bp, bg = FaceMouse()
		local k = func:InvokeServer("Charge")
		local cur = tick()
		
		repeat 
			bg.CFrame = CFrame.new(hrp.Position, CFrame.new(mouse.hit.Position.X, hrp.Position.Y, mouse.hit.Position.Z).Position)
			rs.Heartbeat:Wait() 
		until not Holding or (tick() - cur) >= 5

		local p1, p2 = hrp.Position, CFrame.new(hrp.Position, mouse.hit.Position).lookVector * 25000
		local r = Ray.new(p1, p2)
		local IG = IgnoreList()
		local part, hit = workspace:FindPartOnRayWithIgnoreList(r, IG)

		if not hit or hit and (p1 - hit).Magnitude  > script:GetAttribute("Range") then
			hit = CFrame.new(p1, p2) * CFrame.new(0,0,-script:GetAttribute("Range")).Position
		end

		func:InvokeServer("Fire", p1, hit, part, k)
		bp:Destroy()
		bg:Destroy()
		wait(script:GetAttribute("Cooldown"))
		script:SetAttribute("CanAttack", true)
	end
end

local function ButtonUp(input)

	if hum:GetState() == Dead then return end

	if input.KeyCode == Enum.KeyCode[script:GetAttribute("KeyCode")] and Holding then
		Holding = false
	end
end

local function Shockwave(item)
	local ray = Ray.new(item:WaitForChild("In").Position + Vector3.new(0,4,0), Vector3.new(0,-10,0))
	local ig = IgnoreList()
	local part, hit = workspace:FindPartOnRayWithIgnoreList(ray, ig)
	if part then
		local shockwave = game:GetService("ReplicatedStorage").Kamehameha.Shockwave:Clone()
		shockwave.Position = hit
		shockwave.Parent = workspace.FX
		game.Debris:AddItem(shockwave, 2)
		firetrack.Looped = true
		chargetrack:Stop(0)

		local t = ts:Create(shockwave, TweenInfo.new(0.6, Enum.EasingStyle.Sine), {
			Transparency = 1;
			Size = shockwave.Size * 1.6;
			CFrame = shockwave.CFrame * CFrame.Angles(0,math.rad(180),0) * CFrame.new(0,5.2,0)
		})

		t:Play()
		t.Completed:Connect(function()
			t:Destroy()
			shockwave:Destroy()
		end)
	end
end

local function ExplosionWave(item, t)
	if t.PlaybackState == Enum.PlaybackState.Delayed then		
		Shockwave(item)
	end
end

local function Added(item)
	if item.Name == "Shock" then

		coroutine.resume(coroutine.create(function()

			local n = 1
			while item:isDescendantOf(workspace.FX) do

				item.CFrame = item.CFrame * CFrame.Angles(0,math.rad(n),0)
				if n <= 9 then n = n + 0.5 end


				rs.Heartbeat:Wait()
			end
		end))
	elseif item.Name == "Explosion" then
		item:WaitForChild("In") item:WaitForChild("Out")
		for i,v in pairs(item:GetChildren()) do
			local t = ts:Create(v, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 4, true, 0.1), {
				Size = v.Size * 1.5;
				Transparency = 0.5;
			})

			t:Play()
			Shockwave(item)
			local ss = t:GetPropertyChangedSignal("PlaybackState"):Connect(function()
				ExplosionWave(item, t)
			end)
			t.Completed:Connect(function()
				ss:Disconnect()
				t:Destroy()
				local t = ts:Create(v, TweenInfo.new(0.4, Enum.EasingStyle.Sine), {
					Size = v.Size * 2;
					Transparency = 1;
				})
				
				t:Play()
				t.Completed:Connect(function()
					t:Destroy()
					v:Destroy()
				end)
			end)
		end
	end
end

uis.InputBegan:Connect(ButtonDown)
uis.InputEnded:Connect(ButtonUp)
workspace.FX.DescendantAdded:Connect(Added)

deleteab.OnClientEvent:Connect(function()
	script:Destroy()
end)

this is the local script for it, there is also a server script for it which i can also give, how can i implement it into mobile?

If the script works for holding probably use TouchLongPress?

Where can i put that in the script?

I did read the last lines of your script and saw that you need to detect the down and up clicking; you can add a TextButton and use the MouseButton1Down and MouseButton1Up.
like this:

Button.MouseButton1Down:Connect(ButtonDown)
Button.MouseButton1Up:Connect(ButtonUp)

I tried the script but it needs the keycode part to work since its apart of the function and i dont really know how to remove it without the script breaking

Check if TouchEnabled is true with UserInputService and use the function without needing to use keycode but if it came out as false, use else as keycode.

easier: make a new function only mobile compatible.

Any ability for mobile should be made via ContextActionService, which can be used to make mobile buttons.

In order to create a button we would do:

local function handleAction(Action,State)
   if state == Enum.UserInputState.Begin then
      print('Down')
   else
      print('Up')
   end
end

ContextActionService:BindAction("ActionName", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)

You can read more on it via the document I sent.
Holding down on the screen to trigger an action can be annoying as when they go to turn their camera this function of yours will trigger cause they are holding down on the screen, use buttons. Its better :3

would it be like this?

Almost! We got to give it the action, which is a string, so you can tell what is going on, the action is also displayed on the button that is created so people know what it does:

contextserv:BindAction('ActionNameHere',ButtoneDone,true,Enum.Keycode.H)

To give some context to just help:
The first argument, or arg cause Iā€™m to tired to type, is the Action, this is what the Action value is in the ButtonDone function, Action is also again displayed on the button that is created.
The second arg is a bool, when set to true it creates a mobile button, when false, it doesnt.

After that you can put any keycode you want! Anyone of these keycodes when pressed or unpressed will trigger the ButtonDone function, for instance.

local function func(Action,State)
    print(Action) -> "LaLa"
end

CS:BindAction('LaLa', func, true,Enum.Keycode.F,Enum.Keycode.X,Enum.Keycode.LeftShift)

You can also put Gamepad keycodes in there!
If you have any questions feel free to ask!

Where would i get the actionname from in my script?

ActionName, or Action is just a string, so you can just put a string there

would it be like this? i used it and got this result
robloxapp-20240924-0011028.wmv (1.4 MB)
Screenshot 2024-09-24 001125

Yes that should work! Sorry for the late response!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.