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

This function is not working? But every time I press play my PC crashes.

Asked by 3 years ago
Edited by Ziffixture 3 years ago

This script ain't working and there where no errors:

Code:

local clickDetector = workspace.Button.ClickDetector

function onMouseClick()
    while true do
        local part = Instance.new("Part", game.Workspace)
        part.Transparency = 0.5
        part.Anchored = false
        part.Position = Vector3.new(26.17, 52.86, -15.685)
    end
end

clickDetector.MouseClick:connect(onMouseClick)

Note from Ziffixture:

To enclose your code in blocks, click the Lua icon in the presented tools above in StackEdit.

0
add a wait(). without wait() this script will repeat so fast roblox cant handle it CjayPlyz 643 — 3y
0
use game.RunService.Stepped instead of while true do for this. AlexanderYar 788 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

What’s happening is a common case of script exhaustion. Studio only allows scripts to run for a certian amount of time each frame. If that number is exceeded, Studio will end up crashing due to an exhaustion error. The solution is simple, replace while true do with while wait() do. Replacing the condition in the while loop with wait() will wait a duration of one frame every time the while loop runs, allowing the script to run without exhaustion.

Ad
Log in to vote
0
Answered by
Soban06 410 Moderation Voter
3 years ago

You need to add a wait(). Do it like this:

local clickDetector = workspace.Button.ClickDetector

function onMouseClick()
    while true do
        local part = Instance.new("Part", game.Workspace)
        part.Transparency = 0.5
        part.Anchored = false
        part.Position = Vector3.new(26.17, 52.86, -15.685)
    wait() -- Add this wait
    end
end

clickDetector.MouseClick:connect(onMouseClick)

Hope it helped.

Answer this question