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

Why does this gui crash studios?

Asked by 9 years ago

I made this script, and it crashes my roblox studios! I'm not sure why.. Please let me know!

while true do
local HealthNum = game.workspace.Cake.Humanoid.Health
script.Parent.Text = HealthNum.."/100"
end

2 answers

Log in to vote
1
Answered by 9 years ago

Simple, the while true do loop is attempting to execute without any delay time. Basically it's trying to run multiple operations at once infinitely, and that causes roblox crashes. Obviously the simplest way to fix it is by putting a wait(Seconds) somewhere in the loop, preferably at the beginning or at the end, like so:

while true do
local HealthNum = game.workspace.Cake.Humanoid.Health
script.Parent.Text = HealthNum.."/100"
wait() --This makes the loop wait 1/30th of a second
end

Hope this helped!

Ad
Log in to vote
-1
Answered by 9 years ago

A smarter way to approach this would be to do the following:

Humanoid = game:GetService("Workspace"):WaitForChild("Cake").Humanoid
Humanoid.Changed:connect(function()
script.Parent.Text = Humanoid.Health.."/100"
end)
0
Why are you using GetService? They conveniently made it so that you could access stuff in Workspace simply by putting "workspace.x". funyun 958 — 9y
0
Also this would not contiously update the text like a While loop as im pretty sure he was trying to do. drew1017 330 — 9y

Answer this question