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

Roblox Studio has stopped working when I use a while? [SOLVED]

Asked by
Geko97 29
6 years ago
Edited 6 years ago

I'm trying to make a weapon with which you keep left click until there are no more bullets.

For do that, I have a LocalScript in my gun where I have a variable called "hold"

local hold = false

Below I have the following code

tool.Equipped:Connect(function(mouse)
    gungui:Clone().Parent = player.PlayerGui
    encontrarCuerpo()
    equipar:FireServer(tool.Disparar)
    mouse.Button1Down:Connect(function()
        if balas.Value <= 0 or reload2 == true then
            --Nothing happens
        else
            hold = true
            while hold do
            wait(2)
            local head = game.Workspace[player.Name].Head.CFrame.lookVector
            local mouse = CFrame.new(game.Workspace[player.Name].Head.Position, mouse.Hit.p).lookVector
            difference = (head-mouse)
            local ray = Ray.new(tool.Handle.CFrame.p,(player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit*300)
            local part, position = game.Workspace:FindPartOnRay(ray, player.Character,false,true)
            if difference.magnitude < 1.33 then
                if reloading == false then
                    shootevent:FireServer(tool, position, part)
                    balas.Value = balas.Value -1
                    hold = false
                end
            end
        end
    end
end)

I have a wait of 2 sec and when I press left click instantaneously roblox stops working without wait the 2 sec.

Thanks for read.

0
try lowering the seconds to 0.1. Does it still work? ax_gold 360 — 6y
1
Nope Geko97 29 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

That is quite a simple fix. First the reason why your studio crashes is because if you add a infinite loop like that with no yielding it just destroys it. I can't get too into depth but you get the point. If you want it to work faster and not crash then you can do "wait()" rather than "wait(2)"

1
it still does not work Geko97 29 — 6y
Ad

Answer this question