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

Why does the script kill my game whenever I try to run it?

When the script gets to "while true do" it simply crashes my studio, I added the 5 second wait already just to make sure about what exactly was making the game crash... What is wrong about this?

local player = game.Players.LocalPlayer

repeat wait() until player.Character ~= nil
repeat wait() until player.Character:FindFirstChild("Humanoid")


print("Started working")


script.Parent.MainFrame:WaitForChild("CharacterPortrait")

local imageP = script.Parent.MainFrame.CharacterPortrait.PlayerImage
local level = player.Stats.level.Value
local name = player.Name
imageP.Image = "https://www.roblox.com/bust-thumbnail/image?userId=" ..player.UserId.. "&width=420&height=420&format=png"
script.Parent.MainFrame.CharacterPortrait.Level.Text = level
script.Parent.MainFrame.Name1.Text = name
script.Parent.MainFrame.Name2.Text = name

wait(5)

while true do
	
	
if player.Character:FindFirstChild("Humanoid") ~= nil then
	

script.Parent.MainFrame.HealthBar.HP.Text = "HP: " .. math.floor(player.Character.Humanoid.Health) .. " / " .. player.Character.Humanoid.MaxHealth

	
end
end

3 answers

✓ Accepted answer

Your while loop continuously loops through the character's humanoid. Maybe try adding the word "break" at the end on line 30 to stop the loop after the humanoid is found?

Easy problem.

Solution: Never use while true do without wait(), it's a game crasher.

Try making it while wait(0.1) do or add wait(0.1) somewhere in the loop, and your game will not crash anymore!

Just add a Wait() at the end of the loop :

-- This is an exemple

char = game.Players.LocalPlayer.Character.Humanoid

while true do
	char.WalkSpeed = char.WalkSpeed + 5
	wait(.1)
end

So if you Don't want your script to crash roblox, add a wait at the end of the loop just like i did.

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