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

Problem with my simple hunger system, Whats wrong?

I'm pretty new to the scripting stuff i like building, i tried doing this myself but failed

Its supposed to make your health go down to 0 if the hunger value is 0.

And if your hunger is greater than 100 it will reset to 100 to prevent exploitation

while true do
	wait(10)
	script.Parent.Value = script.Parent.Value - 1
end

while true do
wait(1)
if script.Parent.Value >= 0 then
	for i,v in pairs(game.Players.LocalPlayer) do
	v.Character.Humanoid.Health = 0	
end
end


while true do
wait(1)
if script.Parent.Value > 100 then 
	for i,v in pairs(script.Parent) do
	v.Value = 100	
end
end
end
end

Sorry for taking your time, please tell me if i made a stupid mistake

2 comments

0

what does the output say?

0

Nothing related to the script inside the value

2 answers

✓ Accepted answer

This will not work because of the way coding works. In EVERY coding language, they read it from up to down. So, from that, this script will not print Hello world!

while wait() do
	print('spam')
end
while wait() do
	print('Hello world!')
end

It is stuck printing spam, and it does not read code past that. To prevent a future question you may or may not have, you cannot do this.

print(number)
local number = 10

It will not work. So what you should do is to combine the while loops like in the top example.

while wait() do
	print('spam')
	print('Hello world!')
end

Now it will print spam AND Hello world! So what you should do in your script is this. I'm using coroutines, so they run simultaneously.

local firstloop = coroutine.wrap(function()
	while wait(10) do
		script.Parent.Value = script.Parent.Value - 1
	end
end)
local secondloop = coroutine.wrap(function()
	while wait(1) do
		if script.Parent.Value >= 0 then
   			 for i,v in pairs(game.Players.LocalPlayer) do
				    v.Character.Humanoid.Health = 0 
			end
		end
	end
end)

local thirdloop = coroutine.wrap(function()
	while wait(1) do
		if script.Parent.Value > 100 then 
   			 for i,v in pairs(script.Parent) do
  				  v.Value = 100   
			end
		end
	end
end)

firstloop()
secondloop()
thirdloop()

3 comments

0

Thanks to everyone who helped me i appreciate it so much :D

0

Can you accept one of our answers though?

0

Can you accept one of our answers though?

Hi.

As far as I know, a script can handle only one while true loop. Everything what's bellow under the while true loop will not be compiled. However, in this situation you can fit everything in one loop.

I suggest using a Server script not a local script. LocalPlayer is only accessible via Local Script but you don't need it.

Here is the code:


local Hunger = script.Parent

--I assume that the Hunger value will be located in Player's model (character in workspace)
local Player = Hunger.Parent
local Humanoid = Player:FindFirstChild("Humanoid")

while true do
	wait (10)

	Hunger.Value = Hunger.Value - 1 --Here we decrease the hunger value every 10 sec
	
	if Hunger.Value > 100 then	     --IF it is grater than 100 then it is being set to 100
		Hunger.Value = 100
	end
	
	if Hunger.Value <= 0 then	
		Humanoid.Health = 0	--IF hunger is bellow or equal 0 then we kill the player
	end
end

Create a value, name doesn't matter. Insert this script to it and put everything into StarterPlayer > StarterCharacterScripts

Hope this helps!

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