Hi folks,
I have a script that is working well to create a system where the player places a sticker on a skateboard. In short, they click a button in a window (their sticker book), which sets a value equal to the name of the sticker they clicked, and while they’re holding down the mouse, if they drag over their skateboard (a separate surface Gui) it positions another image in that Gui into the spot they’ve dragged over.
This works well on PC, but not at all on mobile since obviously there is no mouse. I’ve read that mobile is supposed to use a virtual mouse, but I’m not sure if how that can be used. I appreciate any guidance.
Local Script to set the selected sticker:
-- When the player clicks the sticker set player value, and maintain the value while holding the mouse.
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
OllieSticker.MouseButton1Down:Connect(function()
player.GuiData.SelectedSticker.Value = "Ollie"
OllieSticker.TextLabel.Visible = true -- Makes "Sticker Selected" Text appear
end)
end
end)
-- Value back to nil when releasing the mouse.
game:GetService("UserInputService").InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
player.GuiData.SelectedSticker.Value = ""
OllieSticker.TextLabel.Visible = false
end
end)
And a second script to set the sticker’s position on the board:
StickerFrame.MouseMoved:Connect(function(xPos, yPos)
local offset = Vector2.new(math.abs(xPos - StickerFrame.AbsolutePosition.X), math.abs(yPos - StickerFrame.AbsolutePosition.Y - 36))
if SelectedSticker.Value == "Ollie" then -- Player has clicked their sticker and is holding down the mouse, while now over the board Gui: StickerFrame.
PlacedOllie.Position = UDim2.new(0,offset.X,0,offset.Y)
end
end)
Have you looked into the TouchStarted, TouchEnded, and TouchMoved events? You can use the events to determine when a player has begun touching their screen and when they move their finger around on the screen.
Something like this should work, although I haven’t tested it in Studio:
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Touch then
OllieSticker.MouseButton1Down:Connect(function()
player.GuiData.SelectedSticker.Value = "Ollie"
OllieSticker.TextLabel.Visible = true
end)
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Touch then
player.GuiData.SelectedSticker.Value = ""
OllieSticker.TextLabel.Visible = false
end
end)
game:GetService("UserInputService").TouchMoved:Connect(function(input)
local xPos = input.Position.X
local yPos = input.Position.Y
local offset = Vector2.new(math.abs(xPos - StickerFrame.AbsolutePosition.X), math.abs(yPos - StickerFrame.AbsolutePosition.Y - 36))
if SelectedSticker.Value == "Ollie" then
PlacedOllie.Position = UDim2.new(0,offset.X,0,offset.Y)
end
end)
2 Likes
Thank you so much - I have not, and should likely have researched further. (I did a lot of searching on things like “MouseEnter for mobile” - which was probably not right.
I can do some testing with what you wrote; thank you. One question: Would I need to have both my ‘mouse-based’ scripts, and the ‘touch-based’ for this all to function? Are others doing the same? i.e. Programming for all user types?
Probably clear I’m new to this by the rookie questions - appreciate the help.
1 Like
Yes, you will need to support all user input types for touch and mouse.
My example was just of the touch events you can use to achieve your objective.
For not testing your script worked very well; thank you again. The first part was flawless - setting the SelectedSticker.Value while the mouse is held (now while touching).
For the second part - moving the imagebutton on the Gui: “SticerFrame” - Your script worked partially but does not account for the mouse/touch needing to be over the StickerFrame; something handled with a mouse using: StickerFrame.MouseMoved:Connect(function(xPos, yPos)
Not sure if this is possible, but I continue to experiment. Thank you again for the help.
1 Like