-- AutoFarmServer.lua (ServerScriptService)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local toggleEvent = ReplicatedStorage:WaitForChild("ToggleAutoFarm") -- RemoteEvent
local farmZone = workspace:WaitForChild("FarmZone") -- Part
local zoneRadius = math.max(farmZone.Size.X, farmZone.Size.Z) / 2 -- approximate radius
local zoneCenter = farmZone.Position

local REWARD_INTERVAL = 2          -- seconds between rewards
local REWARD_AMOUNT = 10           -- coins per interval
local MAX_ACTIVE = 50              -- optional: limit number of simultaneous autofarmers

-- track active players: player -> {enabled = bool, nextRewardTime = number}
local active = {}

-- ensure player has leaderstats.Coins
local function ensureLeaderstats(player)
    local stats = player:FindFirstChild("leaderstats")
    if not stats then
        stats = Instance.new("Folder")
        stats.Name = "leaderstats"
        stats.Parent = player
    end
    local coins = stats:FindFirstChild("Coins")
    if not coins then
        coins = Instance.new("IntValue")
        coins.Name = "Coins"
        coins.Value = 0
        coins.Parent = stats
    end
    return coins
end

local function isInZone(position)
    -- simple distance check to the zone center in the XZ plane
    local dx = position.X - zoneCenter.X
    local dz = position.Z - zoneCenter.Z
    local dist2 = dx*dx + dz*dz
    return dist2 <= (zoneRadius * zoneRadius)
end

-- Toggle request from client. We always validate server-side.
toggleEvent.OnServerEvent:Connect(function(player, wantOn)
    if type(wantOn) ~= "boolean" then return end

    -- safety checks
    if wantOn then
        -- limit active farm count to prevent abuse (optional)
        local count = 0
        for _ in pairs(active) do count = count + 1 end
        if count >= MAX_ACTIVE then
            -- optionally notify player via a RemoteEvent or skip enabling
            return
        end

        -- check player's character and HumanoidRootPart
        local char = player.Character
        local hrp = char and char:FindFirstChild("HumanoidRootPart")
        if not hrp then return end

        -- ensure they're actually inside the zone
        if not isInZone(hrp.Position) then
            -- reject - client requested while not in zone
            return
        end

        -- enable
        active[player] = {enabled = true, nextRewardTime = tick() + REWARD_INTERVAL}
    else
        -- disable
        active[player] = nil
    end
end)

-- Clean up when player leaves
Players.PlayerRemoving:Connect(function(player)
    active[player] = nil
end)

-- Award loop (server-owned)
spawn(function()
    while true do
        local now = tick()
        for player, state in pairs(active) do
            if not state.enabled then
                active[player] = nil
            else
                -- validate character & position
                local char = player.Character
                local hrp = char and char:FindFirstChild("HumanoidRootPart")
                if not hrp or not isInZone(hrp.Position) then
                    -- player left zone or lost character -> stop farming
                    active[player] = nil
                else
                    if now >= state.nextRewardTime then
                        -- award
                        local coins = ensureLeaderstats(player)
                        coins.Value = coins.Value + REWARD_AMOUNT
                        -- schedule next
                        state.nextRewardTime = now + REWARD_INTERVAL
                    end
                end
            end
        end
        wait(0.5) -- tick rate: half second; adjust for performance vs. responsiveness
    end
end)
2) Local script — put in StarterPlayer > StarterPlayerScripts as AutoFarmClient.lua
lua
Copy code
-- AutoFarmClient.lua (StarterPlayerScripts)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local toggleEvent = ReplicatedStorage:WaitForChild("ToggleAutoFarm")

-- Simple toggle — you can hook this to a GUI button instead
local isOn = false

-- Example usage: press "F" to toggle (for quick testing)
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.F then
        isOn = not isOn
        toggleEvent:FireServer(isOn)
        -- optional: show feedback (e.g., print or update a GUI)
        if isOn then
            print("Requested AutoFarm ON")
        else
            print("Requested AutoFarm OFF")
        end
    end
end)

-- If you want a GUI button, call toggleEvent:FireServer(true/false) from the button callback instead.

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: