⚔ïļCombatService

Service for handling combat actions.

Example usage:

-- 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, amount: number, fromEntity: Entity | nil, knockback: Knockback | 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, amount: number)

Heals the entity for the specified amount.

Last updated