How to make gun for mobile?

How to make gun for mobile devices, im trying to find on youtube and google but didn’t have :sad: , can someone help me please because i really need it.

please reply down below :slightly_smiling_face: .

2 Likes

A gun with shoot button or you tap screen to shoot?

1 Like

i want my game with shoot button.

Then that should be pretty similar to a gun for pc except you change the mouse.Button1Down function to button.MouseButton1Up

1 Like

Do you know how to make one for pc?

1 Like

yes i know make gun for pc but for mobile i don’t know :slightly_frowning_face:

Just do this in the gun script then. Do you already have a script made?

1 Like

yes i already have a script on the gun

local tool = script.Parent
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer

tool.Equipped:Connect(function(mouse)
	
	mouse.Button1Down:Connect(function()
		local Bullet = Instance.new("Part", game.Workspace)
		Bullet.Name = "Bullet"
		Bullet.BrickColor = BrickColor.new("Really red")
		Bullet.Shape = Enum.PartType.Cylinder
		Bullet.Anchored = false
		Bullet.Size = Vector3.new(1, 2, 1)
		
		local ray = cam:ScreenPointToRay(mouse.X, mouse.Y, 100)
		local distance = (tool.Handle - ray.Direction).magnitude
		local speed = 500
		Bullet.CFrame = CFrame.new(ray)
	end)
end)

this is my script.

Ok I will modify this for mobile.

First off make a screen gui in starterGui and make a frame and button (Name the button shoot)

local tool = script.Parent
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local Button = plr.PlayerGui.ScreenGui.Frame.Shoot

tool.Equipped:Connect(function()
	
	Button.MouseButton1Up:Connect(function()
		local Bullet = Instance.new("Part", game.Workspace)
		Bullet.Name = "Bullet"
		Bullet.BrickColor = BrickColor.new("Really red")
		Bullet.Shape = Enum.PartType.Cylinder
		Bullet.Anchored = false
		Bullet.Size = Vector3.new(1, 2, 1)
		
		local ray = cam:ScreenPointToRay(mouse.X, mouse.Y, 100)
		local distance = (tool.Handle - ray.Direction).magnitude
		local speed = 500
		Bullet.CFrame = CFrame.new(ray)
	end)
end)

Hope this helps, if your confused just ask :slight_smile:

but i want the mouse lock on middle screen.

i already make a crossshair.

image

Then you can just enable shiftlock when they do it heres the scipt for that

local tool = script.Parent
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local Button = plr.PlayerGui.ScreenGui.Frame.Shoot

tool.Equipped:Connect(function()

	UserGameSettings.RotationType = Enum.RotationType.CameraRelative

	Button.MouseButton1Up:Connect(function()
		local Bullet = Instance.new("Part", game.Workspace)
		Bullet.Name = "Bullet"
		Bullet.BrickColor = BrickColor.new("Really red")
		Bullet.Shape = Enum.PartType.Cylinder
		Bullet.Anchored = false
		Bullet.Size = Vector3.new(1, 2, 1)
		
		local ray = cam:ScreenPointToRay(mouse.X, mouse.Y, 100)
		local distance = (tool.Handle - ray.Direction).magnitude
		local speed = 500
		Bullet.CFrame = CFrame.new(ray)
	end)
end)
3 Likes

ok i will try and i will tell you if has problem :slightly_smiling_face: .

Use ContextActionService and BindAction for making/binding the controls: ContextActionService | Roblox Creator Documentation
And you can’t enable shiftlock on mobile, and it’s better to use MouseButton1Click on mobile instead of MouseButton1Down, as sometimes it bugs out and doesn’t work.

1 Like

This is actually false, you can enable Mobile Shiftlock by editing the PlayerModule. It does involve forking, however it is very well possible. I did it for my experience.

@Harizhasnan12

I wish you luck on this. I’m not too savvy when it comes to this type of work. One thing I do know is that Mobile actually has a hidden mouse cursor, and the mouse cursor teleports to the location that you tapped on your screen. Technically Mouse.Hit still works, so if you’re firing rays to make a gun, simply cast that way using Mouse.Hit when in first person and you got yourself a working mobile gun.

3 Likes

Can you give me some example or scripts ?

I’m pretty sure mousebutton1click works for both pc and mobile. If not just see if they are on a mobile device and give them a shoot button. How to detect if Player is using a Phone? - #4 by buildthomas

2 Likes

thank you very much it works !!!

1 Like