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

Im trying to make it where if you type "100 Health" then it changes your health to 100?

local box = script.Parent
local text = box
local plr = game.Players

while true do
	if text.Text == "health" then
			plr.Character.Humanoid.Health = 100
		print("Health Has Changed Too 100")
		wait(0.1)
	end
end

the error is "Script timeout: exhausted allowed execution time"

1 comment

1

place the wait() outiside of the if

4 answers

Your loop doesn't yield if the text is not equal to health. Therefore it hangs the game and causes an error.

while true do
    if text.Text == "health" then
            plr.Character.Humanoid.Health = 100
        print("Health Has Changed Too 100")
    end
    wait(0.1)
end

Will always wait, and prevents you from hanging.

Problem:

The reason it won't work is enumerated below:

  1. The reason why it wont work is the function "Wait" only runs whilst the other conditional statement are true! Since it wont get there (because the script reads from top to bottom) it causes an error, and will never run!

You could use a coroutine to avoid that, but it still won't work because of the error!

Solution:

Make sure you put the wait before the if statement and after all you loops (or before)

Fixed Script:

local box = script.Parent
local text = box
local plr = game.Players.PlayerAdded:Connect(function(plr)

while true do -- it's better to use while wait do as it's more efficient and a time saver
wait(1) -- change to your preference!
    if text.Text == "health" then
            plr.Character.Humanoid.Health = 100
        print("Health Has Changed Too 100")
    end
   end
end)

J.CSong

This should do the trick. Instead of a while loop, use the GetPropertyChangedSignal event, since it not only reduces lag, but is wayyyyy more efficient!

local box = script.Parent
local text = box
local plr = game.Players

text:GetPropertyChangedSignal('Text'):Connect(function()
    if text.Text == "health" then
            plr.Character.Humanoid.Health = 100
        print("Health Has Changed Too 100")
        wait(0.1)
    end
end)

*sneakily combines two above codeblocks*

Put this in a LocalScript

local box = script.Parent
local plr = game:GetService("Players").LocalPlayer
local event = game:GetService("ReplicatedStorage"):WaitForChild("HealEvent")

box:GetPropertyChangedSignal('Text'):Connect(function()
	if box.Text == "health" then
		event:FireServer()
	end
end)

Put this in a Script

local event = game:GetService("ReplicatedStorage"):WaitForChild("HealEvent")

event.OnServerEvent:Connect(function(player)
	player.Character.Health = player.Character.MaxHealth
end)

Make a RemoteEvent called HealEvent and put it in ReplicatedStorage.

Explanation coming later

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