I want to make a game that is like skriblio. so I create the gui and tried to connect them. and code the logic I got an error when I try to run my script is there something wrong in my script? the error are in line 12 here is my code
local UserInputService = game:GetService("UserInputService")
local currentTool = "Line" -- Ganti dengan alat default Anda
local currentColor = Color3.new(0, 0, 0) -- Ganti dengan warna default Anda
local canvas = script.Parent -- Ganti dengan objek "canvas" yang sesuai
local function drawLine(startPoint, endPoint)
local line = Instance.new("Part")
line.Anchored = true
line.Material = Enum.Material.SmoothPlastic
line.BrickColor = BrickColor.new(currentColor)
line.Size = Vector3.new((startPoint - endPoint).Magnitude, 0.2, 0.2)
line.CFrame = CFrame.new((startPoint + endPoint) / 2, Vector3.new(endPoint.X, endPoint.Y, 0))
line.Parent = canvas
end
local function drawCircle(center, radius)
local circle = Instance.new("Part")
circle.Anchored = true
circle.Material = Enum.Material.SmoothPlastic
circle.BrickColor = BrickColor.new(currentColor)
circle.Shape = Enum.PartType.Cylinder
circle.Size = Vector3.new(radius * 2, 0.2, radius * 2)
circle.CFrame = CFrame.new(center) + Vector3.new(0, 0.1, 0)
circle.Parent = canvas
end
local function drawFreehand(startPoint, endPoint)
local line = Instance.new("Part")
line.Anchored = true
line.Material = Enum.Material.SmoothPlastic
line.BrickColor = BrickColor.new(currentColor)
line.Size = Vector3.new(0.2, 0.2, (startPoint - endPoint).Magnitude)
line.CFrame = CFrame.new((startPoint + endPoint) / 2, Vector3.new(endPoint.X, endPoint.Y, 0))
line.Parent = canvas
end
local function erase(startPoint, endPoint)
-- Dapatkan semua objek anak di dalam "canvas"
local canvasChildren = canvas:GetChildren()
-- Iterasi melalui semua objek anak dan periksa apakah mereka berada dalam wilayah yang ditentukan
for _, child in ipairs(canvasChildren) do
if child:IsA("BasePart") then
local position = child.Position
if position.X >= startPoint.X and position.X <= endPoint.X and
position.Y >= startPoint.Y and position.Y <= endPoint.Y then
child:Destroy()
end
end
end
end
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local startPoint = UserInputService:GetMouseLocation()
if currentTool == "Line" then
-- Menggambar garis
UserInputService.InputEnded:Wait()
local endPoint = UserInputService:GetMouseLocation()
drawLine(startPoint, endPoint)
elseif currentTool == "Circle" then
-- Menggambar lingkaran
UserInputService.InputEnded:Wait()
local endPoint = UserInputService:GetMouseLocation()
local radius = (startPoint - endPoint).Magnitude
drawCircle(startPoint, radius)
elseif currentTool == "Pen" then
-- Menggambar secara bebas
local prevPoint = startPoint
repeat
local currentPoint = UserInputService:GetMouseLocation()
drawFreehand(prevPoint, currentPoint)
prevPoint = currentPoint
wait()
until not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
elseif currentTool == "Eraser" then
-- Menghapus
UserInputService.InputEnded:Wait()
local endPoint = UserInputService:GetMouseLocation()
erase(startPoint, endPoint)
end
end
end)
my code knowledge is very limited because i'm still learning, what is my problem and what do i need to change?
The error is telling you exactly what is wrong. When you tried to create a CFrame, an object that represents a 3D position and orientation in space, you gave it a Vector2 and the constructor expected a Vector3.
In your
drawLinefunction, you construct a CFrame by averaging the startPoint and endPoint variables, but both of those variables come from UserInputService:GetMouseLocation() which returns a Vector2.A simple answer might be to try to convert the Vector2 into a Vector3, using the same system as your other Vector3s.
But, you have 2D screen coordinates and you are trying to use them to place an object into a 3D space. One way to do this is to raycast into the space and try to find an object that it hits. A large block acting as a canvas would be recommended here. So instead of UserInputService:GetMouseLocation(), try using the Mouse class. It comes with a
Hitproperty that will give you a CFrame of a collision point inside the workspace. You can use that to get your Vector3s.