NumberValue dont update in script

Hello, i’m facing a problem with a numbervalue.

I put a number Value inside the player humanoid, and set its value to 100.

I then made a script that is supposed to work, only problem is the numbervalue is not updating.

function regen(char)

local mana = char:WaitForChild("Humanoid").ManaVal.Value
local maxMana = char:WaitForChild("Humanoid").MaxMana.Value
print(mana)
print(maxMana)
if mana < maxMana then 
	mana +=10 
end
	
 end 

 while true do 
wait(5)
regen(script.Parent)
end

When I change the Mana value manually and set it to 80, the console keeps printing 100, it seems like it’s not updating in the script.
Thank you.

2 Likes

You’re referencing the Value property immediately, it just sets the value into the variables and doesn’t change it in the numbervalues.

Solution: Reference the Value property yourself so it is able to update the Values accordingly

function regen(char)
	local mana = char:WaitForChild("Humanoid").ManaVal
	local maxMana = char:WaitForChild("Humanoid").MaxMana
	print(mana.Value)
	print(maxMana.Value)
	if mana.Value < maxMana.Value then 
		mana.Value +=10 
	end	
end 

while true do 
	wait(5)
	regen(script.Parent)
end
2 Likes

Still does the same thing, I really dont get it.

How are you changing the Value? From the Client or the server? If you’re changing it on the Client, the server can’t detect that the value has been updated and may require a RemoteEvent

2 Likes

I’m changing it on the client, that’s why I think.

So how am I supposed to make it change on the server too ?

image

Simply create a RemoteEvent in ReplicatedStorage, And create a regular script in ServerScriptService with this code

local repStore = game:GetService("ReplicatedStorage")
local remote = repStore:WaitForChild("RemoteEvent")

remote.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hum = char and char:FindFirstChildOfClass("Humanoid")
	if not hum then return end
	hum.ManaVal.Value -= 10
end)

And then in your Localscript, just add a variable to get the Event from the ReplicatedStorage

local remote = game:GetService("ReplicatedStorage").RemoteEvent

And then fire it when you need to decrement

remote:FireServer()

You don’t have to pass in the localplayer yourself, it does so automatically in this case.

I’d also recommend reading this to help you undersatnd more indepth about RemoteEvents and RemoteFunctions and how in the future if you plan on usign them more, you’d to apply sanity checks if the client has to pass in stuff themselves

1 Like