Variable doesnt get recognized?

So ive got an integer value that is placed inside the player.

In a local script i do this

local value = player.Value.Value – 0

Now i do something like this:
Im on phone rn so sorry if typos. So i store the value in a variable. And then update that variable in multiple functions through the script. But my while loop never seems to show the updated value? What am i doing wrong here?

function test()
value += 5
print(value) -- prints 5
end

Button.MouseButton1Down:conncect(function()
value += 5
print(value) -- 5
end)

while true do
print(value) -- always 0
end
1 Like

You misspelled “Connected”, you wrote “conncect” instead. Also, that “white true do” loop would crash your game, add a task.wait():

function test()
value += 5
print(value) -- prints 5
end

Button.MouseButton1Down:Connect(function()
value += 5
print(value) -- 5
end)

while true do
print(value) -- always 0
task.wait()
end

You could have also set it to 0 accidentally in another part of the code.

1 Like

On top of what @TinkyWinkyDev said, your function test() was not even called. The value will remain 0.

Here’s a simplified rewrite:

local value = 0

function test()
	value += 5
	print(value)
end

Button.MouseButton1Down:Connect(test) -- Connect the same function instead. Don't make a duplicate function.

-- The while true do is unnecessary for your current script.
-- If you need the while true do for something else, you might
-- as well consider equivalent and appropriate events first before
-- making a loop.
1 Like

Guys i said i wrote on phone lol

Please re read my post. Assume there is no errors and im calling both functions.

This was not obvious. We assumed that what you have is all your code already. What’s your full script? A (supposed) re-write is not as useful as the actual code.

I dont understand whats not clear.

I have an int value. And even though i update it in 2 functions. Inside the while loop it never shows the updated value and always prints 0 (default)

wait a second how did your pc survive while true do loop with no wait()
but actually the problem is a scope i guess

oh you didnt call the function
maybe on your pc you made the while loop somewhere before the mouse button so it never gets to the mouse button event
also please for the love of god put a wait() your pc will explode if you forget it or maybe use heartbeat inside the run service

Wrote the code on my phone. My issue is more so that the value wont update outside of the scope its in.

Assume that the functions are called. And the while loop is fine. I wrote a shortened example on my phone.

when you get to your pc can you give us the code? maybe if it didnt explode because of the forgot wait() :skull:

1 Like

did you put the while loop before the button1down event?

Don’t save an Object’s Value as a variable, cause you’re defining that as your own number datatype & it can’t properly change back, until you re-define it as something else

What you’d want instead, is only reference the IntValue Object and add 5 whenever you hit your MouseButton1Down Event, and I’d recommend using a Changed Event instead of an infinite loop, so that it properly fires whenever your IntValue detects a change in property

local Plrs = game:GetService("Players")
local Plr = Plrs.LocalPlayer
local IntVal = Plr:WaitForChild("Value")

local function AddAmount()
    IntVal.Value += 5
end

local function DetectChange(NewValue)
    print("New Value Changed To:", NewValue)
end

Button.MouseButton1Down:Connect(AddAmount)
IntVal.Changed:Connect(DetectChange)
1 Like

Thank you. Ill give that a try. And i use a while loop since i have a gold per second mechanic. Where you upgrade gold mines for more gold. So i just threw this in there.

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