local env = (getgenv and getgenv()) or _G if env.AutoSurf and env.AutoSurf.Stop then env.AutoSurf.Stop() end assert(type(getgc) == "function" and type(getfenv) == "function", "AutoSurf: this environment does not expose getgc/getfenv") assert(debug and type(debug.getupvalues) == "function" and type(debug.setupvalue) == "function", "AutoSurf: debug.getupvalues/setupvalue are required") local Players = game:GetService("Players") local RunService = game:GetService("RunService") local UIS = game:GetService("UserInputService") local player = Players.LocalPlayer local surf = game:GetService("ReplicatedStorage"):WaitForChild("Surf", 15) assert(surf, "AutoSurf: Surf folder not found in this game") local controller = player:WaitForChild("PlayerScripts"):WaitForChild("SurfController", 15) assert(controller, "AutoSurf: SurfController not loaded") local Config = require(surf:WaitForChild("Config")) local WaveField = require(surf:WaitForChild("WaveField")) local Boards = require(surf:WaitForChild("Boards")) local Spots = require(surf:WaitForChild("Spots")) local Tutor = require(surf:WaitForChild("BalanceTutor")) local function functionName(fn) if debug.info then local ok, name = pcall(debug.info, fn, "n") if ok and name and name ~= "" then return name end end if debug.getinfo then local ok, info = pcall(debug.getinfo, fn) if ok and info then return info.name end end end local functions = {} local owned = {} for _, value in pairs(getgc()) do if type(value) == "function" then local ok, scope = pcall(getfenv, value) if ok and scope and rawget(scope, "script") == controller then owned[#owned + 1] = value local name = functionName(value) if name then functions[name] = value end end end end local popUp = functions.popUp local window = functions.popUpWindow local pump = functions.pump local inputs = functions.inputs assert(popUp and window and pump and inputs, "AutoSurf: controller functions were not found; this game/build is unsupported") -- Verify the exported popUp closure layout before relying on its state. local function readState() local values = debug.getupvalues(popUp) local heading, forward, position, stats, velocity = values[1], values[2], values[3], values[4], values[5] if typeof(forward) ~= "Vector3" or (forward - Config.FORWARD).Magnitude > 0.01 then error("AutoSurf: popUp closure layout changed; stopped") end if heading ~= nil and typeof(heading) ~= "Vector3" then error("AutoSurf: unexpected heading state; stopped") end if typeof(position) ~= "Vector3" or typeof(velocity) ~= "Vector3" or type(stats) ~= "table" or type(stats.PUMP_COOLDOWN) ~= "number" then return nil end return heading, position, velocity, stats end local api = { Enabled = true } local connections, patches = {}, {} local stopped = false local originalDrive = Tutor.drive local character, mode, wave, heading, position, velocity, stats local balance = Vector2.zero local lastPump, lastPop, nextUpdate = -math.huge, -math.huge, 0 local paddleLeft, paddleRight = false, false local gui = Instance.new("ScreenGui") gui.Name = "AutoSurfControl" gui.ResetOnSpawn = false gui.DisplayOrder = 100 gui.Parent = player:WaitForChild("PlayerGui") local button = Instance.new("TextButton") button.Size = UDim2.fromOffset(240, 42) button.Position = UDim2.new(0, 16, 0, 100) button.BackgroundColor3 = Color3.fromRGB(20, 36, 45) button.TextColor3 = Color3.fromRGB(120, 255, 195) button.Font = Enum.Font.GothamBold button.TextSize = 14 button.Parent = gui local corner = Instance.new("UICorner") corner.CornerRadius = UDim.new(0, 9) corner.Parent = button local function surfMode() character = player.Character if stopped or not api.Enabled or UIS:GetFocusedTextBox() or player:GetAttribute("SurfReplay") or player:GetAttribute("SurfTravelBusy") then return nil end if not character or not character:FindFirstChild("SurfboardActive") then return nil end local humanoid = character:FindFirstChildOfClass("Humanoid") if not humanoid or humanoid.Health <= 0 then return nil end local current = character:GetAttribute("SurfMode") if current ~= "Paddle" and current ~= "Stand" then return nil end -- These board types have separate launch/flight controls. local board = Boards.Get(player:GetAttribute("BoardType")) if board and (board.kite or board.skim or board.foil) then return nil end return current end local function automatedInputs(...) local current = surfMode() if not current then return inputs(...) end if current == "Paddle" then return true, paddleLeft, false, paddleRight end return false, false, false, false end -- Change only controller closure references. Record each slot for restoration. for _, fn in ipairs(owned) do local ok, values = pcall(debug.getupvalues, fn) if ok then for index, value in pairs(values) do if value == inputs then patches[#patches + 1] = { fn, index, value } end end end end assert(#patches > 0, "AutoSurf: no controller input references found") local function assistedDrive(...) if surfMode() == "Stand" then -- The game's existing assist hook owns balance integration. Staying -- below 0.22 leaves a large margin beneath its magnitude-1 wipeout. return balance, Vector2.zero end return originalDrive(...) end function api.Stop() if stopped then return end stopped = true api.Enabled = false for _, connection in ipairs(connections) do connection:Disconnect() end for _, patch in ipairs(patches) do pcall(function() if debug.getupvalues(patch[1])[patch[2]] == automatedInputs then debug.setupvalue(patch[1], patch[2], patch[3]) end end) end if Tutor.drive == assistedDrive then Tutor.drive = originalDrive end gui:Destroy() if env.AutoSurf == api then env.AutoSurf = nil end end local ok, failure = pcall(function() for _, patch in ipairs(patches) do debug.setupvalue(patch[1], patch[2], automatedInputs) assert(debug.getupvalues(patch[1])[patch[2]] == automatedInputs, "AutoSurf: executor could not replace controller inputs") end Tutor.drive = assistedDrive end) if not ok then api.Stop(); error(failure) end env.AutoSurf = api local function update() local now = os.clock() if now < nextUpdate then return end nextUpdate = now + 1 / 30 mode = surfMode() if not api.Enabled then button.Text = "AutoSurf OFF | F6" balance = Vector2.zero return end if not mode then button.Text = "AutoSurf ON | waiting for surf" balance = Vector2.zero return end heading, position, velocity, stats = readState() if not heading then button.Text = "AutoSurf ON | waiting for rider"; return end wave = WaveField.Sample(position, WaveField.Now()) local spot = Spots.Get(Spots.At(position)) local lateral = (position - spot.origin):Dot(Config.LATERAL) local halfWidth = Config.SEGMENTS * Config.SEG_WIDTH / 2 -- A modest shoulder line, with a gradual turn back inside the wave field. local shoulder = math.clamp(-lateral / math.max(halfWidth * 0.65, 1), -1, 1) * 0.25 local desired = (Config.FORWARD + Config.LATERAL * shoulder).Unit local errorAngle = math.atan2(heading:Cross(desired).Y, heading:Dot(desired)) -- Paddle rotation: A adds yaw, D subtracts it. paddleLeft = errorAngle > 0.06 paddleRight = errorAngle < -0.06 if mode == "Paddle" then balance = Vector2.zero button.Text = "AutoSurf ON | catching a wave" if now - lastPop > 0.8 and window(wave) then lastPop = now popUp(wave) end else balance = Vector2.new(math.clamp(-errorAngle * 1.4 / math.max(stats.CARVE_RATE or 2, 0.1), -0.22, 0.22), 0) button.Text = "AutoSurf ON | riding + balancing" if wave.onFace and wave.h > 0.4 and wave.c > 0 and (wave.dhds or 0) < -0.1 and now - lastPump >= math.max(stats.PUMP_COOLDOWN, 0.25) and velocity.Magnitude < wave.c * 1.35 then lastPump = now pump() -- Original function retains the board's cooldown checks. end end end local function toggle() api.Enabled = not api.Enabled balance = Vector2.zero nextUpdate = 0 end connections[#connections + 1] = button.Activated:Connect(toggle) connections[#connections + 1] = UIS.InputBegan:Connect(function(input, processed) if not processed and input.KeyCode == Enum.KeyCode.F6 then toggle() end end) connections[#connections + 1] = RunService.PreSimulation:Connect(function() local success, reason = pcall(update) if not success then api.Stop(); warn("AutoSurf stopped: " .. tostring(reason)) end end) connections[#connections + 1] = player.CharacterAdded:Connect(function() balance = Vector2.zero paddleLeft, paddleRight = false, false lastPump, lastPop = -math.huge, -math.huge end) button.Text = "AutoSurf ON | waiting for surf" print("AutoSurf ready. Mount a normal surfboard; F6 toggles. AutoSurf.Stop() unloads.")