Set a Player Value to an Image Button's Image URL While Holding Down the Mouse Only

I am trying to set a player value to the image URL of a button when the player clicks that button and holds down the mouse. When the player releases the mouse, I would like the player value cleared (back to nil). My code functions well upon the first click of the button, but when I click elsewhere after the release, the value reappears. Put another way, once the mouse button is released the player should have to click the button again to set the value equal to the image url. As currently functioning, even after releasing the button, every subsequent click the image url reappears in the player value. I appreciate any insight.

ImageButton.MouseButton1Down:Connect(function()
  game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
      player.GuiData.SelectedSticker.Value = ImageButton.Image
    end
  end)
  game:GetService("UserInputService").InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
      player.GuiData.SelectedSticker.Value = ""
    end
  end)
end)

coso.rbxm (3.4 KB)

I made some twitches, but you need to adapt it to what you’re doing.

I believe the problem is that the imagebutton is actually utilizing the mouse clicks without considering where it’s clicking. in other words, it ignores where to click, it just does it anyways.

I believe that, to solve this, you would need to put a click detector to the object, and not use the mouse service which is what seems to be what you’re doing (mouse service is for firing or using something, not for activating something in an exact area. If you were to use the mouse service for activation in an exact area, you would need impossiible levels of programming for it, when roblox already has something for it), because I don’t have the full code, I don’t know if it’s right, I’d need you to tell me…

The model I’m giving you is two parts: one that is just there for holding, the other one which is a flat surface with a decal (you can’t put click detectors on decals, they don’t work) and a script that has your code with a little tweaks.

Edit: Ignore the decal, I made the number equivalent of vnehewibvwwpwdjkl

1 Like

You shouldn’t wrap InputService inside of a event. Try the following instead, as buttons have a up and down event built into them:

ImageButton.MouseButton1Down:Connect(function()
      player.GuiData.SelectedSticker.Value = ImageButton.Image
end)
ImageButton.MouseButton1Up:Connect(function()
      player.GuiData.SelectedSticker.Value = ""
end)

@FranSauce
There is no need for a click detector, period. Click detectors are used for 3d objects, not 2d UI.

Also, checking whether the mouse is in a certain bounds is NOT impossible. You can do it using InputService.

2 Likes

@FranSauce - I can’t thank you enough for taking all of the time to create the model for me. I messed with it quite a bit. It got me close but I couldn’t quite get the mouse to set and release correctly. In the end @ThatTimothy’s simple solution got me on the right track. Still - I really appreciate the time and eduction. I’m able to properly place stickers now by clicking an imagebutton in one Gui, then place it on another by releasing on that second Gui. I used the MouseButton1Up function on the second imagebutton (a duplicate of the first but on the new Gui). The effect is like placing a sticker.

I’m relatively new to all of this - Timothy; you’re unknowingly like my Roblox professor by way of posts. Appreciate it.

2 Likes