How can I make a shooting system on Mobile?

Problem
So in my game there is a mobile system that throws a ball when its clicked. (The point of the game is to try to throw balls at people to know people off the map etc.) When I was working on a mobile system for the game, I tried to include a button that throws the ball with the crosshair in the middle. The thing is the crosshair is UDIM2 and my ball throwing system is rendered in CFrame

Here is an example of the layout
Dont worry about the jump button

When your not on mobile everything works as intented. The script takes your mouse position and uses that.

Example of the working pc version

local UserInputServices = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local Character = player.Character
local Mouse = player:GetMouse()

local ThrowEvent = game.ReplicatedStorage:WaitForChild("ThrowEvent")

local Hold = false
local Cooldown = false
local Time = 3 --Example time

local starterGui = player.PlayerGui

UserInputServices.InputBegan:Connect(function(input)
	if UserInputServices.MouseEnabled then
		if input.UserInputType == Enum.UserInputType.MouseButton1 and Cooldown == false then
			Character.Humanoid:LoadAnimation(script.Throw):Play()ThrowEvent:FireServer(Mouse.Hit)
			print(Mouse.Hit)
			Hold = false
			Cooldown = true
			wait(Time)
			Cooldown = false
			Time = 0
			repeat wait() until not Hold
		end
	end
end)

Everything works as intended but on the mobile version its a little different

local player = game.Players.LocalPlayer
local Character = player.Character
local ThrowEvent = game.ReplicatedStorage:WaitForChild("ThrowEvent")
local mouse = player:GetMouse()

local Hold = false
local Cooldown = false
local Time = 0

local crosshair = player.PlayerGui.MainUI.Aim

local targetPosition = Vector3.new(0, 0, 0) -- Initialize target position with a default value

local zPosition = 10 -- Set the default Z-coordinate

local cam = workspace.CurrentCamera
local inset = game.GuiService:GetGuiInset().Y

local pos = crosshair.AbsolutePosition  + crosshair.AbsoluteSize / 2 
local ray = cam:ScreenPointToRay(pos.X, pos.Y) -- use if IgnoreGuiInset is disabled in your UI


-- Function to handle mouse button click
script.Parent.MouseButton1Click:Connect(function()
	if Cooldown == false then 
		Character.Humanoid:LoadAnimation(script.Throw):Play()ThrowEvent:FireServer(ray)
		Hold = false
		Cooldown = true
		wait(Time)
		Cooldown = false
		Time = 0
		repeat wait() until not Hold
	end
end)

The errors I get for the second script are
Position is not a valid member of ray

The script that receives these 2 is

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ThrowEvent = ReplicatedStorage.ThrowEvent
local BallsToThrow = ReplicatedStorage:WaitForChild("BallsToThrow")

local gravity = 300
local throwspeed = 200

ThrowEvent.OnServerEvent:Connect(function(Player, mouse)
	wait(0.28)

	local ballName = Player.Index.Ball.Value
	local ballToThrowTemplate = BallsToThrow:FindFirstChild(ballName)

	if ballToThrowTemplate then
		local ThrowItem = ballToThrowTemplate:Clone()  -- Clone the template
		local offset = Vector3.new(0, 2.5, 0) -- Adjust the offset as needed
		local offsettest = Vector3.new(4, 6.5, 0)
	
		ThrowItem.CFrame = CFrame.new(Player.Character:FindFirstChild("Head").Position + offset, mouse.Position)
		ThrowItem.Velocity = ThrowItem.CFrame.LookVector * throwspeed
		ThrowItem.CanCollide = false
		ThrowItem.Parent = Player.Character  -- Assign the parent to the local player's character
		ThrowItem.Anchored = false
		ThrowItem.Name = "Clone"


		local attachment = Instance.new("Attachment", ThrowItem)
		local vectorforce = Instance.new("VectorForce", ThrowItem)

		vectorforce.Force = Vector3.new(0, ThrowItem:GetMass() * gravity, 0)

		ThrowItem:SetNetworkOwner(Player)
	end
end)

Dont worry of the ball types

Where the scripts are located
Script 1 = StarterCharacterScripts
Script 2 = player.PlayerGui.MainUI.MobileThrow
Script 3 = ServerScriptService

The closest forum I can find to this was

[How do i shoot a gun on mobile with ImageLabel]
(How do i shoot a gun on mobile with ImageLabel)

but it was not completed.

Example of ball throwing system

If you know the game arsenal, that’s what I’m basing this off on right now.

Any help is appreciated :pray::pray:

1 Like

Making the ball get thrown directly into the direction of the camera using something like, Camera.CFrame.LookVector should work, and yes it will also always aim to the middle of the camera when you do this. I did this ALOT of times before and it worked perfectly fine, atleast for me…

2 Likes

I changed the script a little bit to add the look vector, but when I added it in I still got the same error. How do I use Lookvector?

Script

local player = game.Players.LocalPlayer
local Character = player.Character
local ThrowEvent = game.ReplicatedStorage:WaitForChild("ThrowEvent")
local mouse = player:GetMouse()

local Hold = false
local Cooldown = false
local Time = 0

local crosshair = player.PlayerGui.MainUI.Aim

local cam = workspace.CurrentCamera

script.Parent.MouseButton1Click:Connect(function()
	if Cooldown == false then 
		Character.Humanoid:LoadAnimation(script.Throw):Play()

		local rayCFrame = CFrame.new(cam.CFrame.X, cam.CFrame.Y + cam.CFrame.LookVector * 300) -- Assuming a maximum throw distance of 300 studs

		ThrowEvent:FireServer(rayCFrame)
		Hold = false
		Cooldown = true
		wait(Time)
		Cooldown = false
		Time = 0
		repeat wait() until not Hold
	end
end)

try this

Script

local player = game.Players.LocalPlayer
local Character = player.Character
local ThrowEvent = game.ReplicatedStorage:WaitForChild("ThrowEvent")
local mouse = player:GetMouse()

local Hold = false
local Cooldown = false
local Time = 0

local crosshair = player.PlayerGui.MainUI.Aim

local cam = workspace.CurrentCamera

script.Parent.MouseButton1Click:Connect(function()
	if Cooldown == false then 
		Character.Humanoid:LoadAnimation(script.Throw):Play()

		local rayCFrame = CFrame.new(cam.CFrame.Position,cam.CFrame.Position+cam.CFrame.LookVector * 300) -- Assuming a maximum throw distance of 300 studs

		ThrowEvent:FireServer(rayCFrame)
		Hold = false
		Cooldown = true
		wait(Time)
		Cooldown = false
		Time = 0
		repeat wait() until not Hold
	end
end)

Works great but one final issue. The ball seems to move inward to the player? Heres how it looks like on my phone.

(sorry if the quality is bad)

local player = game.Players.LocalPlayer
local Character = player.Character
local ThrowEvent = game.ReplicatedStorage:WaitForChild("ThrowEvent")
local mouse = player:GetMouse()

local Hold = false
local Cooldown = false
local Time = 0

local crosshair = player.PlayerGui.MainUI.Aim

local cam = workspace.CurrentCamera

script.Parent.MouseButton1Click:Connect(function()
	if Cooldown == false then 
		Character.Humanoid:LoadAnimation(script.Throw):Play()

		local rayCFrame = CFrame.new(cam.CFrame.X, cam.CFrame.Y + cam.CFrame.LookVector * -300) -- Assuming a maximum throw distance of 300 studs

		ThrowEvent:FireServer(rayCFrame)
		Hold = false
		Cooldown = true
		wait(Time)
		Cooldown = false
		Time = 0
		repeat wait() until not Hold
	end
end)

Error is Attempt to perform arithmetic (add) on number and vector3
Never heard of this before

My bad

local player = game.Players.LocalPlayer
local Character = player.Character
local ThrowEvent = game.ReplicatedStorage:WaitForChild("ThrowEvent")
local mouse = player:GetMouse()

local Hold = false
local Cooldown = false
local Time = 0

local crosshair = player.PlayerGui.MainUI.Aim

local cam = workspace.CurrentCamera

script.Parent.MouseButton1Click:Connect(function()
	if Cooldown == false then 
		Character.Humanoid:LoadAnimation(script.Throw):Play()

		local rayCFrame = CFrame.new(cam.CFrame.Position, cam.CFrame.Position + cam.CFrame.LookVector * -300) -- Assuming a maximum throw distance of 300 studs

		ThrowEvent:FireServer(rayCFrame)
		Hold = false
		Cooldown = true
		wait(Time)
		Cooldown = false
		Time = 0
		repeat wait() until not Hold
	end
end)

I see what’s wrong now…

local player = game.Players.LocalPlayer
local Character = player.Character
local ThrowEvent = game.ReplicatedStorage:WaitForChild("ThrowEvent")
local mouse = player:GetMouse()

local Hold = false
local Cooldown = false
local Time = 0

local crosshair = player.PlayerGui.MainUI.Aim

local cam = workspace.CurrentCamera

script.Parent.MouseButton1Click:Connect(function()
	if Cooldown == false then 
		Character.Humanoid:LoadAnimation(script.Throw):Play()

		local rayCFrame = cam.CFrame * CFrame.new(0,0,100)

		ThrowEvent:FireServer(rayCFrame)
		Hold = false
		Cooldown = true
		wait(Time)
		Cooldown = false
		Time = 0
		repeat wait() until not Hold
	end
end)

If this doesn’t work, change the 100 in CFrame.new(0,0,100) to -100

2 Likes

Yes It works! God bless :pray::pray::pray:
(Just change it to -100)

1 Like

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