Help on making a whiteboard

I am wondering how to make a whiteboard. So far, I have this code:

local Held = false
script.Parent.MouseEnter:Connect(function(x, y)
	print("Entered")
	script.Parent.MouseButton1Down:Connect(function()
		print("Clicked")
		Held = true
		while Held == true do
			local Tem = script.Parent.Template:Clone()
			local gui_X = script.Parent.Parent.AbsolutePosition.X
			local gui_Y = script.Parent.Parent.AbsolutePosition.Y
			local offset = Vector2.new(math.abs(x - gui_X), math.abs(y - gui_Y - 36))
			print(offset.X, offset.Y)
			Tem.Position = UDim2.new(0, offset.X, 0, offset.Y)
			Tem.Visible = true
			Tem.Parent = script.Parent
			wait(0.1)	
			script.Parent.MouseButton1Click:Connect(function()
				Held = false
				print("RELEASED I THINK")
	end)
		end
	end)

end)

It keeps offsetting by a little, or by a lot. I got a part of the code from a Dev hub article. Please let me know if I put it into the wrong section or something.

Well, I’m not entirely sure what your issue is, but I can spot a few issues in your code right off the bat:

  1. x and y will never update. These variables are only set upon the event firing, because they are intended to represent the point at which the game detected the mouse entering the GUI element. It does not represent the mouse’s current position on the screen. For that, you can use the :GetMouseLocation() function of UserInputService. Alternatively, you can get the mouse object from the player using :GetMouse(), and then read the X and Y properties of the returned Mouse object. (UserInputService | Roblox Creator Documentation, Mouse | Roblox Creator Documentation)
  2. There is no use for MouseEnter in this situation. It appears the reason you used it here was to get the mouse position, but as I clarified in issue 1, this is not a proper way to do so.
  3. You are using MouseButton1Click incorrectly. MouseButton1Click is the event for when a player presses down and then lifts the left mouse button on a GUI. The event for the left mouse button lifting off of a GUI element is MouseButton1Up. (GuiButton | Roblox Creator Documentation, GuiButton | Roblox Creator Documentation)

If these changes fix your problem, great. Otherwise, some more information on exactly what is going wrong would be helpful, especially a GIF or video of the issue.

On number 3 I just noticed I accidentally wrote MouseButton1Click, I just changed it to MouseButton1Up. A GIF would be kinda useless but ok. This is only a small project, so there are lots of bugs and stuff like that.

1 Like

I also got it to work on gui, but not on surfacegui

That’s strange. Judging by your video, you got the events to register properly on the SurfaceGui, and I can’t think of any other common complications that might arise from copying the code from the ScreenGui over to the SurfaceGui. When you say it doesn’t work, what do you mean? What is it that isn’t working?

I also noticed that in the video, the first two problems I pointed out were still not fixed. Did you fix them after recording? Or do you need any more clarification on what I meant?

i fixed them after the video, and the dot keeps offsetting from my mouse.

and i actually quit the project lol