Home GloomShop Videos Contact Games

How To Make A KILL COUNTER in UEFN

Script Below

KIll Count Multi

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Game }
using { /Verse.org/Simulation/Tags }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /UnrealEngine.com/Temporary/Diagnostics }
using {/UnrealEngine.com/Temporary/UI }

# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.

# A Verse-authored creative device that can be placed in a level
PlayerStats := class< unique>():
    var Eliminations : int = 0
    var Deaths : int = 0
    var CurrentDistance : int = 0

    AddElim(e : int):void =
        set Eliminations += e

    AddDeath(d : int):void =
        set Deaths += d
    
    SetCurrentDistance(distance : int):void =
        set CurrentDistance += distance
    
    GetElimCount():int =
        return Eliminations
    
    GetCurrentDistance():int =
        return CurrentDistance

spawner := class(tag){}

TextForUI< localizes>(InText : string) : message = "Kills: {InText}"

killcountmulti := class(creative_device):

    var playerMap : [player]PlayerStats = map{}

    InitSpawners():void =
        Spawners := GetCreativeObjectsWithTag(spawner{})
        for (Obj : Spawners):
            if (Spawner := player_spawner_device[Obj]):
                Spawner.SpawnedEvent.Subscribe(OnPlayerAdded)

    # Runs when the device is started in a running game
    OnBegin< override>()< suspends>:void=
        # TODO: Replace this with your code
        Print("Kill ui device has started")
        InitSpawners()
        GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerRemoved)

    
    OnPlayerAdded(NewPlayer : agent): void =
        Print("Player Added")
        if (PlayerObj := player[NewPlayer]):
            if (PlayerExists := playerMap[NewPlayer]):
            
            else:
                if (set playerMap[PlayerObj] = PlayerStats{}):
                    if(AgentStats := playerMap[PlayerObj]):
                        Print("Player Elims: {AgentStats.GetElimCount()}")
                        if (FortChar:fort_character := NewPlayer.GetFortCharacter[]):
                            FortChar.EliminatedEvent().Subscribe(OnEliminated)
                            var UIButton : button_loud = button_loud{}
                            if (PlayerUi := GetPlayerUI[PlayerObj]):
                                MyCanvas : canvas = canvas:
                                    Slots := array:
                                        canvas_slot:
                                            Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.0}, Maximum := vector2{X := 0.5, Y := 0.0}}
                                            Offsets := margin{Top := 100.0, Left := 50.0, Right := 50.0, Bottom := 50.0}
                                            Alignment := vector2{X := 0.5, Y := 0.5}
                                            SizeToContent := true
                                            Widget := UIButton
                                PlayerUi.AddWidget(MyCanvas)
                                UIButton.SetText(TextForUI("{AgentStats.GetElimCount()}"))
    
    OnEliminated(Result:elimination_result):void =
        Eliminator := Result.EliminatingCharacter
        Eliminated := Result.EliminatedCharacter
        if (FortCharacter := Eliminator?, EliminatorAgent := FortCharacter.GetAgent[]):
            AddElimination(EliminatorAgent)
        if (FortCharacter := Eliminated, EliminatedAgent := FortCharacter.GetAgent[]):
            AddDeath(EliminatedAgent)
                

    AddElimination(Agent : agent):void =
        if(PlayerObj := player[Agent]):
            if(AgentStats := playerMap[PlayerObj]):
                var UIButton : button_loud = button_loud{}
                if (PlayerUi := GetPlayerUI[PlayerObj]):
                    MyCanvas : canvas = canvas:
                        Slots := array:
                            canvas_slot:
                                Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.0}, Maximum := vector2{X := 0.5, Y := 0.0}}
                                Offsets := margin{Top := 100.0, Left := 50.0, Right := 50.0, Bottom := 50.0}
                                Alignment := vector2{X := 0.5, Y := 0.5}
                                SizeToContent := true
                                Widget := UIButton
                    PlayerUi.AddWidget(MyCanvas)
                Print("Adding Elimination")
                AgentStats.AddElim(1)
                UIButton.SetText(TextForUI("{AgentStats.GetElimCount()}"))

    
    AddDeath(Agent : agent):void =
        if(PlayerObj := player[Agent]):
            if(AgentStats := playerMap[PlayerObj]):
                Print("Adding Death")
                AgentStats.AddDeath(1)
                


    OnPlayerRemoved(PlayerLeave : player):void =
        if(ActualPlayer := playerMap[PlayerLeave]):
            var TempPlayerMap:[player]PlayerStats = map{}
            for(Key -> Value : playerMap, Key <> PlayerLeave):
                set TempPlayerMap = ConcatenateMaps(TempPlayerMap, map{Key => Value})
            
            set playerMap = TempPlayerMap