Making mouse accurate in a drawing board

  1. What do you want to achieve? Keep it simple and clear!

The drawing dots being drawn accurately in the mouse’s position

  1. What is the issue? Include screenshots / videos if possible!

https://gyazo.com/fb799581aafcea0f706bf3ce756b5369

the drawing dots are not accurate to the mouse position, the accuracy also changes depending on what side you draw for some reason.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried loooking for a fix, couldn’t find any.

The script that fires the remote event, and the important one because it fires out the position.
local mouse = game.Players.LocalPlayer:GetMouse()
local remoteevent = game.ReplicatedStorage.Draw

local x
local y

local pressed = false

mouse.Button1Down:Connect(function()
	pressed = true
end)

mouse.Button1Up:Connect(function()
	pressed = false
end)

mouse.Move:Connect(function()
	if mouse.Target then
		local model = mouse.Target:FindFirstAncestorOfClass("Model")
		if model then
			if model:FindFirstChild("Drawing") then
				x = mouse.X
				y = mouse.Y
				if pressed == true then
					local frame = model.Drawing.SurfaceGui.Frame
					local color = script.Colour.Value
					local pos = UDim2.new(-0.2, x, 0, y)
					remoteevent:FireServer(color, pos)
				end
			end
		end
	end
end)

The script that gets the fired values and converts them into the drawing dots:

local remoteevent = game.ReplicatedStorage.Draw
local frame = script.Parent.SurfaceGui.Frame

    remoteevent.OnServerEvent:Connect(function(plr, color, pos)
    		local dot = Instance.new("Frame")
    		dot.Name = "Dot"
    		dot.Parent = frame.dots
    		dot.BackgroundColor3 = color
    		dot.Size = UDim2.new(0, 7, 0, 5)
    		dot.BorderSizePixel = 0
    		dot.Position = pos
    	
    end)

Set pos to UDim2.new(0,x,0,y).
This should solve the problem.

Also, keep in mind that mouse is deprecated (old) and you should use UserInputService instead.

didnt fix it, thanks for trying to help anyway!

You could probably do some fancy math tricks to get a proper position:

local relativePos = (surfaceGui.Parent.CFrame*CFrame.new(surfaceGui.Parent.Size/2)):Inverse() * CFrame.new(mouse.Hit.p)
local surfacePos = UDim2.new(0, -relativePos.X * surfaceGui.PixelsPerStud, 0, -relativePos.Y * surfaceGui.PixelsPerStud)

You may have to replace relativePos.X with relativePos.Z or other such adjustments depending on what surface your surfacegui is on, the code is designed to work on the front face

This also assumes the frame takes up the entire parts surface, not just a part of it

just tried it out, it outputs CFrame is not a valid member of PlayerMouse “Instance”, would you mind helping me to fix that?

edit: fixed that but still doesnt draw on the proper position:https://gyazo.com/1487f746e6acc248d78a600f694e2606

Oh I forgot to change that, just change mouse.CFrame to mouse.Hit, I was using a part as a testing “mouse”

Already fixed that, as I stated in the last post, but the mouse is still not accurate

Ive edited in the new solution into the original post, sorry for the lack of testing

gonna try this later, thank you so much for helping and as I bet this is the actual solution, im gonna mark it as it! kuddos to you!

1 Like