I have a perfectly working script that controls a grid placement system for a building game I am working on. I’m very proud of it and yet stuck on the last thing I need to complete. I can not figure a way past this one issue and I an hoping for a hero to open my mind to a solution.
The Problem…
On a PC this placement system relies on a mouse. A brick will follow the mouse cursor and when the mouse is clicked, it then fires to the server to place that brick and then offsets the one visible on the local client preparing for fast clicking and building. I am using “context action service” to bind a key press to a part in order to change it’s rotation… (“like the R Key”) HOWEVER on a Mobile device I do not have a keyboard & I can not bind the “context action service” to a GUI being pressed. I don’t see why I can’t connect them but I accept that I can not.
Next
Moving forward…I have created a button for mobile users to use in order to place a brick. And a button for each rotation and they all function perfectly as far as the questions go… does it place a brick? and does it rotate the brick? HOWEVER when I press a GUI to place or rotate the part, it also reads the mouse.Hit location. This causes the brick to jump from it’s prefered location and appear directly underneath the GUI I just pressed. So if I pressed the “place” GUI then the part will jump to the worldspace behind the GUI and then run the placement code there. The same effect happens for the “rotate” button.
I’ve tried…
userInputService.inputbegan (input, gameProcessedEvent) I understand the game processedevent is used to determine weather you clicked onto a GUI or not, But no matter how many times I come at it, I can not “stop” the click through. it registers in the worldspace and fires all the code.
I really do hope someone has some fresh idea;s b.c the problem solving part of my brain is trying to quit and I wont let it. I’m stuck for weeks now.
Is it possible that you could just make a function to rotate the part, then connect the ContextActionService and the Gui.MouseButton1Click event to play the function?
I can’t seem to figure a way to bind the context action service to the gui being pressed. So I made a separate function that just performs the same function as the bind-able contextactionservice. That works great too! except, pressing the GUI also registers in the world space and that is what I want to stop.
When I am on a mobile device, I can use my tap/drag to position the brick where I want it. When I press the GUI to place that brick, I need to stop the click through OR maybe come up with a system to track the current location and tell the server to place it there instead of behind the GUI into the world space. I just don’t know
Sorry, I should be more specific on my second question. So the script recognizes the mobile player touching the GUI and changes the position of your part to be under the GUI?
If this is the case, you could check if the player’s finger is on the GUI or not by using MouseEnter and MouseLeave for the placing GUI. If the player’s finger is on the GUI, don’t change the position of the part
The position of the client-side brick reads from a “runservice”. So it is automaticly chasing the mouse.Hit. I was not able to figure a way to use MouseEnter or MouseLeave to overcome this either. On Mobile Device the MouseEnter “Also” reads a mouse.Hit behind the GUI. where on a PC the cursor floats in and out returning true/false. Mobile just doesn’t have the hover feature and therefor is registering as a click or a touchstarted in this case.
To quote someone, highlight a piece of text from their message, then it should say “Quote” once you let go of your mouse.
What you could do is something like this:
local PartLocked = false
local RotateButton = script.Parent
function RotatePart()
-- rotation stuff here
end)
RotateButton.MouseEnter:Connect(function()
CanPlace = true
end)
RotateButton.MouseLeave:Connect(function()
CanPlace = false
end)
RunService.RenderStepped:Connect(function()
task.wait(0.05)
if PartLocked == false then
RotatePart()
end
end)
So there will be a delay in updating the position by 0.05 of a second. However, this will give the script time to determine if the mouse is on a GUI or not. You may be able to decrease this time to a normal task.wait() without a number inside the parenthesis, and then the delay will be minimal.
Basically, the MouseEnter and MouseLeave events will toggle on/off if the part can be rotated. You can use this same concept for moving the part.
So I am trying to trace the logic here to achieve an understanding of this. This is an interesting mechanical solve with code, but I am still a little lost. Maybe a small walk through of the logic could make it click in my head. I see the set up of the MouseEnter/MouseLeave and I can understand what that means. While tap is held down, the condition = true. release the button and it’s now false.
If I understand the total code idea here, then It would have me set up the mouse Enter/Leave connections like a light switch & then place an additional check inside the RunService. This check would confirm if the rotate value was true or not during each loop through it’s task scheduler. Then the RunService would rotate the part or skip by the rotate code. If I’m correct, kindly confirm that for me. (wont this by like a rapid fire rotate so long as the GUI is held down? I suppose a debounce in the MouseLeave would control that…) What’s to stop the part from jumping over to the most recent mouse.Hit as soon as the RunService loops through again?
I can see how we are making a sort of light switch in the mouse Enter/Leave code, but I can not see how this will stop the part from jumping over to the mouse.Hit. Maybe the task.wait() is enough delay to control this? Won’t it inevitably appear behind the GUI that was pressed on the next RunService Loop?.
Maybe I could create a few IntValues to store the X,Y,Z of the local brick and somehow reference it for the local brick to appear there once the rotate switch is false again? That seems like an overkill, but Roblox Lua has it’s limits. As you can see, it’s like I’m close to understanding, but just having trouble wrapping my mind around this one.
I greatly appreciate any advice to get me through this one. Others have these same issues too. I’ve read all about it b4 posting here. I bet it’s the most simple thing that I am just over complicating. But here we are, just the same.
The MouseLeave and MouseEnter events act as a way to toggle on/off the ability to rotate. If your RunService is firing before the MouseLeave or MouseEnter events update, try adding a task.wait() before the “if” statement which checks if your mouse is on the GUI or not.