Script doesn't work (No errors)

Am making a Hunger Script rn but it just doesn’t work and doesn’t show any errors any help is appreciated!! (btw the attribute is created on another script)

Script:

local Module = require(game.ReplicatedStorage.Modules.Hunger)

game.Players.PlayerAdded:Connect(function(plr)
	print("yes")
	while task.wait(0.1) do
		plr:SetAttribute("Hunger", plr:GetAttribute("Hunger") - Module.HungerDrain)
		if plr:GetAttribute("Hunger") <= Module.HungryStart then
			plr:SetAttribute("Hungry", true)
		else
			plr:SetAttribute("Hungry", false)
		end
		if plr:GetAttribute("Hunger") <= Module.MinHungerValue then
			plr:SetAttribute("Hunger", Module.MinHungerValue)
		end
		if plr:GetAttribute("Hunger") >= Module.MaxHungerValue then
			plr:SetAttribute("Hunger", Module.MaxHungerValue)
		end
	end
end)

Module Script:

local HungerValues = {
	
	StartingHungerValue = 100;
	
	MaxHungerValue = 100;
	
	MinHungerValue = 100;
	
	HungerDrain = 0.01;
	
	HungryStart = 20
	
}

return HungerValues

1 Like

You should Initialize the Attributes first.

local Module = require(game.ReplicatedStorage.Modules.Hunger)

game.Players.PlayerAdded:Connect(function(plr)
	print("yes")

	plr:SetAttribute("Hunger", Module.MaxHungerValue)
	plr:SetAttribute("Hungry", false)

	while task.wait(0.1) do
		plr:SetAttribute("Hunger", plr:GetAttribute("Hunger") - Module.HungerDrain)
		if plr:GetAttribute("Hunger") <= Module.HungryStart then
			plr:SetAttribute("Hungry", true)
		else
			plr:SetAttribute("Hungry", false)
		end
		if plr:GetAttribute("Hunger") <= Module.MinHungerValue then
			plr:SetAttribute("Hunger", Module.MinHungerValue)
		end
		if plr:GetAttribute("Hunger") >= Module.MaxHungerValue then
			plr:SetAttribute("Hunger", Module.MaxHungerValue)
		end
	end
end)
2 Likes

the attribute is created on another script and your changes didn’t work, thanks for trying

1 Like

You should at least wait in the script until the attributes are created.

1 Like

Where the Script is located?
If it’s a Local Script inside Workspace then you can’t use it.

1 Like

i did that and it still didnt work, btw i put print in the loop and it prints out but it just doesnt decrease

1 Like

server script and serverscriptservice in a folder

1 Like

min hunger value is 100 and starting is 100.

1 Like

Also make the while true do loop in a task.spawn()

1 Like

it is actually crazy i never noticed that also what is task.spawn?

1 Like

a task.spawn is like a script inside a script. So anything inside a task.spawn would run while the rest of the script is running, so it’s good for while loops so they don’t yield and stop the script. It’s like a coroutine.

Sorry if my explanation was bad lol, you can search it up if you want.

2 Likes

then, what is the difference between task.spawn and coroutines???

i dont know if im going to be honest

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.