RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"The toggle script is toggling false and true at the same time, it should toggle between them"
The script provided for toggling between `false` and `true` with the press of a key (in this case, the "E" key) is intended to switch the state of `getgenv().farmer` between `false` and `true` every time the key is pressed. However, the comment at the end suggests a misunderstanding, as the script is correctly designed to toggle the state between `false` and `true` with each key press, not at the same time. Here's a clarified version of the script with added comments for better understanding: ```lua -- Auto Attack Function -- getgenv().farmer = false; -- Initialize the global variable 'farmer' to false print("Initial farmer state: ", getgenv().farmer) -- Print the initial state of 'farmer' -- This loop will continuously check the state of 'farmer' while task.wait() do if getgenv().farmer then -- If 'farmer' is true, execute the attack command game.ReplicatedStorage.Remotes.Weapon.TakeDamage:FireServer() print("Attacking...") -- Debugging: Print a message to confirm the attack command is executed end end -- Toggle Function -- local UserInputService = game:GetService("UserInputService") getgenv().farmer = getgenv().farmer or false -- Ensure 'farmer' is initialized to false if not already set -- Function to toggle the state of 'farmer' local function toggleFarmer() getgenv().farmer = not getgenv().farmer -- Toggle the state of 'farmer' print("Farmer is now: ", getgenv().farmer) -- Print the new state of 'farmer' for confirmation end -- Connect the toggle function to the key press event UserInputService.InputBegan:Connect(function(input, gameProcessed) if gameProcessed then return end -- If the key press is part of the game's UI interaction, do nothing if input.KeyCode == Enum.KeyCode.E then -- If the 'E' key is pressed toggleFarmer() -- Call the function to toggle 'farmer' end end) ``` This script correctly toggles the `farmer` variable between `false` and `true` each time the "E" key is pressed. The misunderstanding might have arisen from the expectation of simultaneous toggling, which is not how toggle functionality works. Toggling means switching between two states with each action (in this case, each key press), not activating both states at the same time.