How do i shoot a gun on mobile with ImageLabel

Basically I want the bullet that gets created to shoot in the direction of where the Crosshair (ImageLabel) is, Unfortunately I can’t get the script to work for some reason…

My local script for gun:

local RemoteEvent2 = replicatedStorage:WaitForChild("ShotEvent2")
local MobileCrosshair = player.PlayerGui.Mobile.Crosshair

ShootMobile.TouchTap:Connect(function()
		if gun.Ammo.Value > 0 then
			RemoteEvent2:FireServer(BulletAimer.Position, gun.Handle.Orientation, MobileCrosshair.Position)

			gun_Shot:Play()
			gun.Ammo.Value += -1
		else
			empty_Clip:Play()
		end
	end)

My bullet create script:

local RemoteEvent2 = replicatedStorage:WaitForChild("ShotEvent")

RemoteEvent2.OnServerEvent:Connect(function(player, BulletAimerPos, gunOr, CrossHairPos)

	local bullet = Instance.new('Part')
	bullet.Name = "Bullet"
	bullet.Parent = game.Workspace
	bullet.Shape = Enum.PartType.Cylinder
	bullet.Size = Vector3.new(0.5,0.25,0.5)
	bullet.BrickColor = BrickColor.new('Silver')

	local damageScript = serverStorage:FindFirstChild('Damage'):Clone()
	damageScript.Parent = bullet

	local damageValue = damageScript:WaitForChild("Dmg")
	damageValue.Value = 5


	local distance = (CrossHairPos - BulletAimerPos).magnitude
	local speed = 200
	bullet.CFrame = CFrame.new(BulletAimerPos, CrossHairPos)
	bullet.Velocity = bullet.CFrame.LookVector * speed
	bullet.Orientation = gunOr + Vector3.new(0, -90, 0)

end)

Picture example: (Crosshair in the middle of screen)

Video example:

2 Likes

Maybe instead of using the crosshair as the direction you could use the cameras direction? It would make it so the bullet will fly towards whatever direction the camera is facing

4 Likes

How would I be able to script that? I’m not to sure how to put that in the script.

1 Like

Oh hello! What is Instance “ShootMobile”, did you define it in your script?

1 Like

Instead of putting ShootMobile.TouchTap:Connect(function() you put MobileCrosshair.MouseButton1Click:Connect(function(). Try this.

1 Like

I think your problem is the fact that you try to use a GUI 2d position as the destination. Instead of using MobileCrosshair.Position, try using Vector3.new(MobileCrosshair.Position.X, MobileCrosshair.Position.Y, maxDist)(maxDist is where the bullet should stop moving and despawn)

1 Like

Yes I did define it, this is what it is:

local ShootMobile = player.PlayerGui.Mobile.Shoot
1 Like

He doesn’t want it to spawn a bullet when the player presses the crosshair, rather when the player presses the Shoot button.

Try this:

local RemoteEvent2 = replicatedStorage:WaitForChild("ShotEvent2")
local MobileCrosshair = player.PlayerGui.Mobile.Crosshair

ShootMobile.MouseButton1Click:Connect(function()
	if gun.Ammo.Value > 0 then
		RemoteEvent2:FireServer(BulletAimer.Position, gun.Handle.Orientation, MobileCrosshair.Position)

		gun_Shot:Play()
		gun.Ammo.Value += -1
	else
		empty_Clip:Play()
	end
end)

I ended up getting confused here, he hadn’t defined what ShootMobile was.

Ok, but that’s pc… not mobile I need to be able to tap that button on my phone and it will shoot a bullet with that local script the gun has.

You can go to Test and choose the device.

My roblox is in Portuguese Brazil, sorry if you can’t understand.
image

Here is the link for the video, If you more in depth I can edit the video and post it on youtube and share the link with you!

Video: (It’s not a virus I promise)

I don’t have acess for your video in drive.

Try this, the video will be done processing soon!

1 Like