I made a drawing system for a game I am working on, and it works perfectly; I’m just worried about potential performance problems.
The drawing system works by creating a new frame and positioning it at the mouse location when the mouse is held down and moving.
Video of the Drawing System in Action:
I am just worried about the amount of frames it is creating, as this could lead to memory issues and cause the game to crash.
Here is the drawing script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")
local mouseInside = false
local mouseDown
script.Parent.Bounds.MouseEnter:Connect(function()
mouseInside = true
end)
script.Parent.Bounds.MouseLeave:Connect(function()
mouseInside = false
end)
mouse.Button1Down:Connect(function()
mouseDown = true
end)
mouse.Button1Up:Connect(function()
mouseDown = false
end)
uis.InputChanged:Connect(function(input, processed)
if processed == false then
if input.UserInputType == Enum.UserInputType.MouseMovement or Enum.UserInputType.Touch then
if mouseDown then
if mouseInside == true then
local draw = Instance.new("Frame")
draw.AnchorPoint = Vector2.new(0.5,0.5)
draw.BorderSizePixel = 0
draw.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
draw.Size = UDim2.new(0.03,0,0.03,0)
local aspect = Instance.new("UIAspectRatioConstraint", draw)
local screenSize = workspace.CurrentCamera.ViewportSize
local mouseLocation = uis:GetMouseLocation()
local frameAbsPosition = script.Parent.AbsolutePosition
local frameAbsSize = script.Parent.AbsoluteSize
local relativeX = mouseLocation.X - frameAbsPosition.X
local relativeY = mouseLocation.Y - frameAbsPosition.Y
local drawingWidth = draw.AbsoluteSize.X
local drawingHeight = draw.AbsoluteSize.Y
draw.Position = UDim2.new(0, relativeX, 0, relativeY - 50)
draw.Parent = script.Parent
end
end
end
end
end)
game.ReplicatedStorage.Remotes.Client.TweenDrawingGui.OnClientEvent:Connect(function(show)
if script.Parent.Parent ~= game.ReplicatedStorage.Guis then
script.Parent.Visible = true
if show == true then
script.Parent.Position = UDim2.new(0.5,0,2,0)
script.Parent.Rotation = 60
game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(0.7,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = UDim2.new(0.5,0,0.5,0), Rotation = 0}):Play()
wait(0.4)
script.Parent.Paper:Play()
elseif show == false then
game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(0.7,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = UDim2.new(0.5,0,2,0), Rotation = 60}):Play()
task.wait(0.5)
script.Parent:Destroy()
end
end
end)
Any help would be greatly appreciated. Thank you!