Problem with IntValue

Hey everyone,

I have a problem with IntValue

I have a script that every 0.3 seconds choose random number and increasing If it’s a positive number and else but It’s not decreasing It’s only Increasing

Any Idea why It’s happening?

Can we see the script if possible?

1 Like

Sure,
I feel like I’m missing something

local value = game.ReplicatedStorage:WaitForChild("StockPrice",60)

while wait(0.3) do
	local RandomValue = math.random(-50,2)
	print(RandomValue)
	if tonumber(RandomValue) >= 0 then
		print("Up")
		value.Value += RandomValue
	elseif tonumber(RandomValue) <= 0 then
		print("Down")
		value.Value -= RandomValue
	end
end

You may send us a piece of your code. Note that using `math.random(int0, int1) will only choose an integer. Same for IntValues. Only place integers inside.

1 Like

I’m not sure I’ve used += or -= because I had problems with that. Possibly try replacing value.Value += RandomValue with value.Value = value.Value + RandomValue and value.Value = value.Value - RandomValue?

1 Like

Your issue is that when the number is negative youre subtracting it but when its positive youre adding it

5 + 5 = 10
5 - -5 = 10

If you simply make them both += your problem will be solved

2 Likes

Does it print the random number? I bet yes but not sure about that

1 Like

+= and -= are basically just quicker & more modern ways of doing value.Value = value.Value + RandomValue I believe

1 Like

They are. I’ll have to test them again in the future, but last time I tried using it with ROBLOX’s Lua, there were some problems. I think @PapaBreadd has the solution though;

thanks for helping everyone…

()

1 Like

You have a sign issue: you have to add randomValue in all cases since if you do 13 - (-2) = 13 + 2 so it will never decrease !

make your code shorter if you can and add a += for the down one

local value = game.ReplicatedStorage:WaitForChild("StockPrice",60)

while wait(0.3) do
	local RandomValue = math.random(-50,2)
	print(RandomValue)
	if tonumber(RandomValue) >= 0 then
		print("Up")
		value.Value += RandomValue
	else
		print("Down")
		value.Value += RandomValue   --- add a plus here because  1 - -1 = 1  and 1 + -1 = 0
	end
end
1 Like