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