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
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.
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.
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.
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
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)
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.