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

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

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.

2 comments

0

add a wait(). without wait() this script will repeat so fast roblox cant handle it

0

use game.RunService.Stepped instead of while true do for this.

2 answers

✓ Accepted answer

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.

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.

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