Simple Script isn't working

I want to make an automation button feature in a clicker game.

It’s not working when it seems correct to me, this has happened quite a few times whilst trying to script UI.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local StarterGUI = game:GetService("StarterGui")
local ScreenGUI = StarterGUI:WaitForChild("ScreenGui")
local autoPunch = ScreenGUI:WaitForChild("AutoPunch")

local deb = false
local auto = false


autoPunch.MouseButton1Click:Connect(function()

	if auto == false then
	
		auto = true
		
		while auto == true do

		Player.leaderstats.Punches.Value += 1
		
		wait(1)

		auto = false
		end
	end
end)

The script is a local script and is placed under ScreenGUI.

remove this part from the while loop auto = false

I removed this and it didn’t work, I modified the the function a bit and it still didn’t work.

autoPunch.MouseButton1Click:Connect(function()
	
	if auto == false then
		auto = true
	else
		auto = false
	end

	if auto == false then
		
		while auto == true do

		Player.leaderstats.Punches.Value += 1
		
		wait(1)
		
		end
	end
end)

move the while loop outside and below the function

Still doesn’t work… Do you think this might be an error outside of the script? Because when I changed the function to this

autoPunch.MouseButton1Click:Connect(function()
	
	if auto == false then
		auto = true
		print(auto)
	else
		auto = false
		print(auto)
	end
		
end)

It didn’t end up printing anything.

yeah, it’s most likely an outside problem

	auto = true
	
	while auto == true do

	Player.leaderstats.Punches.Value += 1
	
	wait(1)

	auto = false
	end

Why this? If you make auto = true, then add 1 to the stats, then wait(1) then make auto= false it would be the exact same as

	auto = true
	Player.leaderstats.Punches.Value += 1
	task.wait(1)
	auto = false

It’s just a simple debounce and you’re immediately changing auto from true to false.

wait(1) has been deprecated so use task.wait(1)

Hello! Not sure if you’ve solved this yet.

However, the reason this does not work is because you’re referencing the GUI in StarterGui. This is not the same as the player’s own copy. To find that you need to look into their PlayerGui located under their player object. Hopes this helps!

2 Likes

Tysm, absolute lifesaver! I wish I knew this before :sweat_smile:

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