Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this print multiple times instead of one?

Asked by 6 years ago

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
0
Is it an infinite loop or only a certain amount of times? warfighter10000 4 — 6y
0
a infinite loop Fsxfighter265 10 — 6y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago

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
Ad

Answer this question