Script always reads NumberValue as 100 even after it changes

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want the script inside the tool to read a NumberValue inside the character. If the NumberValue is 100 then it will make the player say that it isn’t hungry, if it’s below 100, sum 5 to the NumberValue, using math.min to not make it go over 100.

  1. What is the issue? Include screenshots / videos if possible!

When the script reads the NumberValue, it’s always 100 even after it changes.
Here i made the script print the NumberValue every time the game thinks it’s 100, like such

local Chat = game:GetService("Chat")
local Tool = script.Parent

local enabled = true
local bites = 0

function onActivated()
	if not enabled then
		return
	end

	enabled = false
	Tool.GripForward = Vector3.new(.995, -.0995, -0)
	Tool.GripPos = Vector3.new(.3, -.8, 1.55)
	Tool.GripRight = Vector3.new(0, 0, 1)
	Tool.GripUp = Vector3.new(0.0995, .995, 0)

	Tool.Handle.DrinkSound:Play()

	wait(0.8)

	local char = Tool.Parent
	local humanoid = char:FindFirstChild("Humanoid")
	local hunger = char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger")

	if humanoid and hunger then
		if bites < 4 then
			if char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value ~= 100 then
				bites = bites + 1
				char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value = math.min(100, char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value + 5)
			elseif char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value == 100 then
				Chat:Chat(char.Head, "I'm full now.")
				print(hunger.Value)
			else
				warn("Eating Error, destroying tool...")
				Tool:Destroy()
			end
		else
			Tool:Destroy()
		end
	end

	Tool.GripForward = Vector3.new(-.989, 0, .149)
	Tool.GripPos = Vector3.new(.55, -.1, -.1)
	Tool.GripRight = Vector3.new(-.149, 0, -.989)
	Tool.GripUp = Vector3.new(0, 1, 0)

	enabled = true
end

script.Parent.Activated:Connect(onActivated)

and when i try using the tool it prints 100 even though the value is 96.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried always using

char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value

on every reference to the hunger value instead of using

local hunger = char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger")
hunger.Value

I also tried looking on the developer hub but didn’t really know how to phrase it correctly and didn’t find anything relevant.

The whole code is inside a tool.
Here is the whole script

local Chat = game:GetService("Chat")
local Tool = script.Parent

local enabled = true
local bites = 0

function onActivated()
	if not enabled then
		return
	end

	enabled = false
	Tool.GripForward = Vector3.new(.995, -.0995, -0)
	Tool.GripPos = Vector3.new(.3, -.8, 1.55)
	Tool.GripRight = Vector3.new(0, 0, 1)
	Tool.GripUp = Vector3.new(0.0995, .995, 0)

	Tool.Handle.DrinkSound:Play()

	wait(0.8)

	local char = Tool.Parent
	local humanoid = char:FindFirstChild("Humanoid")
	local hunger = char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger")

	if humanoid and hunger then
		if bites < 4 then
			if char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value ~= 100 then
				bites = bites + 1
				char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value = math.min(100, char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value + 5)
			elseif char:FindFirstChild("HungerHandler"):FindFirstChild("Hunger").Value == 100 then
				Chat:Chat(char.Head, "I'm full now.")
			else
				warn("Eating Error, destroying tool...")
				Tool:Destroy()
			end
		else
			Tool:Destroy()
		end
	end

	Tool.GripForward = Vector3.new(-.989, 0, .149)
	Tool.GripPos = Vector3.new(.55, -.1, -.1)
	Tool.GripRight = Vector3.new(-.149, 0, -.989)
	Tool.GripUp = Vector3.new(0, 1, 0)

	enabled = true
end

script.Parent.Activated:Connect(onActivated)

The hunger value goes down every 25 seconds when walking or staying still, and goes down 3 times faster while running. Here’s the code that affects the hunger (located in StarterCharacterScripts)

local hunger = script.Hunger

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")

local isSprinting = char:FindFirstChild("isSprinting")
local isStarving = script:FindFirstChild("isStarving")

local RunService = game:GetService("RunService")

function hungerTimer()
	local timer = 25
	
	while true do
		while not isSprinting.Value and timer > 1 do
			timer = timer - 1
			wait(1)
		end
		
		timer = timer - 3
		wait(1)
		
		if timer < 1 then
			if hunger.Value > 0 then
				hunger.Value = hunger.Value - 1
				timer = 10
			else
				isStarving.Value = true
				while hunger.Value < 1 do
					humanoid.Health = humanoid.Health - 2
					wait(2)
				end
					isStarving.Value = false
					timer = 23
			end
		end
	end
end

coroutine.resume(coroutine.create(hungerTimer()))

I have no idea as to why this is happening, maybe it’s my fault (I’m a beginner) or it’s roblox’s fault since this makes no sense to me whatsoever.

Can you please clarify which, if any, of the scripts are local scripts?

Try checking what the value is on the server’s side by pressing this button in Home tab while in game:
image

Changes done locally won’t replicate to the server. Since the PizzaScript is on the server and the HungerHandler is on the client, the PizzaScript won’t see the changes done to the hunger by the HungerHandler script.

1 Like

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