Solved Message in radius (Lua)

Discussion in 'Rust Development' started by SwipoStyle, Jul 20, 2015.

  1. I looked at the plugin Deathnotes
    Code:
    for ply in BasePlayer.activePlayerList:                if self.distance(ply.transform.position, vpos) <= float(PLUGIN['MESSAGES RADIUS']):
    I tried to make myself
    Code:
    for a in player.activePlayerList do
            if Vector3.Distance(a.transform.position, player.transform.position) <= 300.0 then
                rust.SendChatMessage(a, player.displayName, msg, player.UserID)
            end
        end
     
  2. Calytic

    Calytic Community Admin Community Mod

    It would be helpful to add the error you are receiving when attempting to run this.

    You cannot currently enumerate over a List Collection with a for loop in lua. Referenced Teleportation

    Code:
    local PlayerListEnumerator = global.BasePlayer.activePlayerList:GetEnumerator()
       
        -- Iterate through the online player list and check for a match.
        while PlayerListEnumerator:MoveNext() do
        -- Do stuff here
            local displayName = string.lower( PlayerListEnumerator.Current.displayName )
        end
    Also from Teleportation found when Vector3 is referenced, a fully qualified classname is used:

    Code:
    UnityEngine.Vector3.Distance
    instead of just Vector3.Distance. May not be necessary anymore but worth a shot.

    And finally, you can use
    Code:
    rust.UserIDFromPlayer(PlayerListEnumerator.Current)
    to grab the userID .
     
  3. Thank you very much for the help
    I want to share my code, local text chat, hopefully to someone it is will be useful
    Code:
    function PLUGIN:OnPlayerChat(arg)
        local msg = arg:GetString(0, "text")
        local star = string.sub(msg, 0, 1)
        local msg = string.sub(msg, 2, -1)
        if star == '*' then
            local player = arg.connection.player
            local PlayerListEnumerator = global.BasePlayer.activePlayerList:GetEnumerator()
            while PlayerListEnumerator:MoveNext() do
                local a = PlayerListEnumerator.Current
                local a_pos = PlayerListEnumerator.Current.transform.position
                if UnityEngine.Vector3.Distance(a_pos, player.transform.position) <= 300.0 then
                    rust.SendChatMessage(a, player.displayName, msg, rust.UserIDFromPlayer(player))
                end
            end
            return true
        end
    end
    How to use:
    Input in chat '*hello world'
     
    Last edited by a moderator: Jul 22, 2015