Issue with changing the number

Ok so heres my script

local page = 0
local button = script.Parent

button.MouseButton1Down:Connect(function)
page = page+1
end)

if page == 1 then
script.Parent.Parent.Label.Text = "Page 1"

end

if page == 2 then
script.Parent.Parent.Label.Text = "Page 2"

end

if page == 3 then
script.Parent.Parent.Label.Text = "Page 3"

end

while true do
wait(0.1)
print(page)
end

So my issue is when I click next it changes the page number to 2 or whatever, but the text doesnt change…

This is because you are saying the if statements outside of the function, Try putting theif page == 1 then etc into the MouseButton1Down function!

local page = 0
local button = script.Parent

button.MouseButton1Down:Connect(function)
page = page+1

if page == 1 then
script.Parent.Parent.Label.Text = "Page 1"

end

if page == 2 then
script.Parent.Parent.Label.Text = "Page 2"

end

if page == 3 then
script.Parent.Parent.Label.Text = "Page 3"

      end
end) --Notice how the if statements are in the function?

while true do
wait(0.1)
print(page)
end

The if statements are only being ran once meaning that the text will be whatever the page variable is at the time of the script running.

In this case this code below is being run first so page would be equal to 1 at the time of the if statements.

button.MouseButton1Down:Connect(function)
     page = page+1
end)

You can do this if you want.

button.MouseButton1Down:Connect(function)
	 page += 1
	 script.Parent.Parent.Label.Text = "Page " .. tostring(page)
end)
1 Like

here u go

local page = 0
local button = script.Parent

button.MouseButton1Down:Connect(function)
    page = page+1
    wait(0.2)
    script.Parent.Parent.Label.Text = "Page "..tostring(page)
end)

while true do
    wait(0.1)
    print(page)
end
1 Like

You don’t need the wait(0.2) in this instance. If anything it looses performance with no benefit.

1 Like

it will work in the both ways :roll_eyes:

Boy oh boy if you make a while true do without a wait it kinda crashes …

I meant the wait for this…

button.MouseButton1Down:Connect(function)
    page = page+1
    wait(0.2)
    script.Parent.Parent.Label.Text = "Page "..tostring(page)
end)

And about the wait loop, you should use RunService instead as it is more efficient. Because they are using print I am assuming they just have the wait loop for diagnosis.

1 Like