Help With Player Mouse

Hey! I need help with a function: when the player is holding the left mouse button down, something repeats , placing a new part where the cursor is, for example, for an art game. When they aren’t, it stops repeating.

Thanks!

local holding = false
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

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

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

while wait() do
    print(holding, mouse.Hit.Position)
end
local Holding = false
local Mouse = game.Players.LocalPlayer:GetMouse()

Mouse.Button1Down:Connect(function()
    holding = true
    while holding = true and wait do
           --code
          local Part = instance.new("Part",workspace)
          Part.Size = Vector3,new(1,1,1)
          Part.Anchored = true
    end
end)

Mouse.Button1Up:Connect(function()
    holding = false
end)

The problem with this script is that the parts will only show for the client.

Slight mistake, do wait() :wink:

1 Like

How could I make it so it only makes the part appear when the mouse is on the baseplate (so players can’t make towers)?

Use remote events and brodcast an event on the client. Create a new RemoteEvent in ReplicatedStorage call it "Draw".

--local script:
game.ReplicatedStorage.Draw:FireServer(mouse.Hit.Position)
--server script:
game.ReplicatedStorage.Draw.OnServerEvent:Connect(function(Player,Position)
     --code
          local Part = instance.new("Part",workspace)
          Part.Size = Vector3,new(1,1,1)
          Part.Anchored = true
          Part.Position = Position
end)

So I’ve done something similar to this before:


What I did:
I made an invisible (unanchored) part, set the network ownership to the player, and moved it based on the mouse using Mouse.Hit.p as the part’s position (in a loop).
Then, on the server script, I made a loop that kept cloning parts in the same position as the invisible part, which let me create parts that still showed on server-side.

This would create lag, because it’s remote event spam (if it’s in a loop).

1 Like

You could check the part’s position using an if-statement, or you could set the part’s Position.Y to always be on the ground.

Try waiting before setting the parent and just resize and turn it to look at the mouse cursor. This will improve the drawing and not as many squares in the line.

set the y position of anything that is drawn to 1.

Part.Position = Vector3.new(Part.X,1,Part.Y)

Also if you can, try making this:
https://i.gyazo.com/9036f617436abe963904bf5eb92d391f.mp4
it is a better way to draw and if someone moves their mouse really fast it still draws a line.