Cannot change IntValue with script

I would like to change the IntValue in a block that I touch, then have that number display on the block. My script won’t change the IntValue or the text in my TextLabel. I have a block with an IntValue in it and a Surface Gui with TextLabel in it. What am I doing wrong?
I’ve also tried this in Local Scripts and in buttons with TouchInterest. Nothing will change the InValue.
My script:

local value = script.Parent.Number.Value

local text = script.Parent.SurfaceGui.TextLabel.Text

local function OnTouch(hit)

  value = value + 1

  text = value

  script.Parent.Material = "Neon"

  wait(1)

  script.Parent.Material = "Plastic"

end

script.Parent.Touched:Connect(OnTouch)
7 Likes

This should be run as a script in server script service.
After you try this, please post any errors you are getting, this will make it much easier to fix.

Additional info:
Please indent, it makes code much easier to read.
Also, put your code inside three backticks ```. It properly formats and adjusts code, and is also quite helpful.

I hope this helps,
Someperson576

There are no errors. What are the rules for communicating with intvalues and the like? Do I need to do anything special or non-obvious?

For some reason this:

local value = script.Parent.Number.Value

does not work, but this:

local value = script.Parent.Number

and adding .Value afterwards when changing the value does work. I ran into this issue before, but I’m not sure why that is the case. So instead of:

value = value + 1

You should try this:

value.Value = value.Value + 1

I would also change value to something less tricky for when the time your code gets lengthy you don’t confuse yourself.

17 Likes

First, I tried this with click detectors. I want one button to be pressed to make the number go up, and the other button makes the number go down. The number is shown on a surface GUI. What happened when I pressed the up button is the Gui would show the number going up one by one (the IntValue in the Explorer would show no change). Then, if I pressed the down button the number would go down one by one, but, starting at the original number entered into the IntValue before running the game, no matter how high I had gotten the number with the up button (once again, the IntValue in Explorer would not change). Press the up button again and it would start off where I had left off with the up button, not the down button. These are the scripts I used:

--up button

local block = script.Parent
local ClickDetector = block.ClickDetector
local num = block.Parent.Number.Value

ClickDetector.MouseClick:Connect(function()
	num = num +1
	block.Parent.FishDropper.SurfaceGui.TextLabel.Text = num
	
	
end)

--down button

local block = script.Parent
local ClickDetector = block.ClickDetector
local num = block.Parent.Number.Value

ClickDetector.MouseClick:Connect(function()
	num = num - 1
	block.Parent.FishDropper.SurfaceGui.TextLabel.Text = num
	
	
end)

YOU ARE A LIFE SAVER! Thank you, thank you, thank you! Problem solved.

1 Like

I’m going to elaborate on this with an explanation of WHY this works.

When you declare variable to be script.Parent.Number.Value if the value is 42, the variable will forever be 42, unless you declare it again, as Value is a property of an IntValue.

By stating, variable = variable + 1, you are increasing variable from 42 to 43, then 43 to 44 and so forth, and not modifying the IntValue, as variable was set to have the same value as the IntValue.

In order to make the IntValues value change, you need to set Value, by using IntValue.Value = ...

12 Likes

Thanks, and I rememebered why after posting, just didn’t feel like editing, lol.

In short, script.Parent.Number.Value is just the number reference, as opposed to the actual object and its value (script.Parent.Number).