# CombatService

Example usage:

```lua
-- Game of red light green light (don't move when red!)
local lightIsGreen = false
-- Toggle from red to green and green to red
function swapLightColor()
    local nextLightColorIsGreen = not lightIsGreen
    local lightColor = "GREEN"
    if not nextLightColorIsGreen then
        lightColor = "RED"
    end
    MessageService.broadcast("Light changed: " .. lightColor .. "!")

    -- Wait briefly before changing to "red"
    if lightIsGreen then
        task.wait(1)
    end
    -- Swap light color
    lightIsGreen = nextLightColorIsGreen
end

function getLightWaitTime()
    local waitTime = 1.5 + math.random() * 4
    if lightIsGreen then
        waitTime = 5 + math.random() * 5
    end
    return waitTime
end

-- Function to change light color on an interval
function startLightLoop()
    while (task.wait(getLightWaitTime())) do
        swapLightColor()
    end
end

local lastPlayerPosition = {}
-- Function to constantly check if a player has moved
function startMovementLoop()
    while task.wait(0.1) do
        for i, player in PlayerService.getPlayers() do
            local entity = player:getEntity()
            if not entity then
                lastPlayerPosition[player] = nil
                continue
            end
            
            -- Check if player moved when light is red
            if not lightIsGreen and lastPlayerPosition[player] ~= nil then
                local distance = lastPlayerPosition[player] - entity:getPosition()
                if distance.Magnitude > 3 then
                    CombatService.damage(entity, 100)
                end
            end
            if lightIsGreen then
                lastPlayerPosition[player] = entity:getPosition()
            end
        end
    end
end

task.spawn(startLightLoop)
task.spawn(startMovementLoop)
```

### Functions

#### damage(entity: [Entity](/scripting/bedwars-scripting/objects/entity.md), amount: number, fromEntity: [Entity](/scripting/bedwars-scripting/objects/entity.md) | nil, knockback: [Knockback](/scripting/bedwars-scripting/objects/knockback.md) | nil)

Deals the specified amount of damage to an entity. Use the optional `fromEntity` parameter to specify the source of the damage. Use the optional `knockback` parameter to specify the applied knockback multipliers.

#### heal(entity: [Entity](/scripting/bedwars-scripting/objects/entity.md), amount: number)

Heals the entity for the specified amount.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.easy.gg/scripting/bedwars-scripting/services/combatservice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
