Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this print multiple times instead of one?

why does it print multiple times instead one that i'm trying to do, and how can i fix this?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = game.Workspace.Part

while true do
		mouse.TargetFilter = part
		part.Position = mouse.Hit.p
		mouse.Button1Down:connect(function()
			print(true)
			return
		end)
	wait()
end

2 comments

0

Is it an infinite loop or only a certain amount of times?

0

a infinite loop

1 answer

That connect method does not yield so there is no reason for the script to not make that connection every single loop. This is VERY bad it makes new functions each time which eats memory.

Also return cannot break any environment except the current function, so that one does nothing. You should separate these things.

Assuming you want the mouse to constantly be on the part until click:

--be more careful with initalization
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local part = workspace:WaitForChild("Part")
mouse.TargetFilter = part

local clicked = false
mouse.Button1Down:Connect(function() clicked=true end)

repeat
	part.Position = mouse.Hit.p
	wait()
until clicked==true

This discussion is preserved as it was. New answers, comments, and votes are closed.