Client
Client is storing variables and running functions on all connected user devices (clients). It contains everything needed for the local simulation.
Client is nil in the Server execution context.
Properties
Triggered when pressing action 1 button. (space bar by default with a keyboard)
Client.Action1 = function() print("action1") end
Triggered when releasing action 1 button. (space bar by default with a keyboard)
Client.Action1Release = function() print("action1 released") end
Triggered when pressing action 2 button. (left click by default when using a mouse)
(turned off when Pointer is hidden)
Client.Action2 = function() print("action2") end
Triggered when releasing action 2 button. (left click by default when using a mouse)
(turned off when Pointer is hidden)
Client.Action2Release = function() print("action2 released") end
Triggered when pressing action 3 button. (right click by default when using a mouse)
(turned off when Pointer is hidden)
Client.Action3 = function() print("action3") end
Triggered when releasing action 3 button. (right click by default when using a mouse)
(turned off when Pointer is hidden)
Client.Action3Release = function() print("action3 released") end
Triggered when the "analog pad" changes position.
The "analog pad" maps to the mouse movements when using a mouse. When using a touch screen, it is activated when moving the finger after a touch down that isn't caught by any other screen component (like DirectionalPad).
The function is called only when the dx or dy value changes, not continuously while moving in a direction.
The function receives 2 arguments dx & dy to represent the delta compared to the previous position. When both dx & dy are equal to 0.0, it means the pad is back to idle state.
(turned off when Pointer is hidden)
-- DEFAULT IMPLEMENTATION -- (these functions can be redefined) Client.AnalogPad = function(dx, dy) Player.LocalRotation = Rotation(0, dx * 0.01, 0) * Player.LocalRotation Player.Head.LocalRotation = Rotation(-dy * 0.01, 0, 0) * Player.Head.LocalRotation local dpad = require("controls").DirectionalPadValues Player.Motion = (Player.Forward * dpad.Y + Player.Right * dpad.X) * 50 end Client.DirectionalPad = function(x, y) Player.Motion = (Player.Forward * y + Player.Right * x) * 50 end
This function is triggered when receiving an Event.
Events are useful to establish communication between the Server and all connected Players.
Client.DidReceiveEvent = function(event) -- do something with the event -- respond (if it makes sense in the situation) local response = Event() response.content = "Here's my response!" response:SendTo(event.Sender) end
Triggered when the "directional pad" changes direction.
The "directional pad" maps to the direction keys when using a keyboard (WASD by default). When using a touch screen, it is by default represented by a virtual pad on the left side of the screen.
The function is called only when the x or y value changes, not continuously while holding a direction.
The function receives 2 arguments x & y to represent 8 possible directions and idle state:
0,0 -> IDLE
0,1 -> UP, 1,0 -> RIGHT, 0,-1 -> DOWN, -1,0 -> LEFT
√2,√2 -> UP/RIGHT, √2,-√2 -> DOWN/RIGHT, -√2,-√2 -> DOWN/LEFT, -√2,√2 -> UP/LEFT
The norm (or length) of the (x,y) vector is always 1.0, or 0.0 when idle.
-- DEFAULT IMPLEMENTATION -- (these functions can be redefined) Client.DirectionalPad = function(x, y) Player.Motion = (Player.Forward * y + Player.Right * x) * 50 end Client.AnalogPad = function(dx, dy) Player.LocalRotation = Rotation(0, dx * 0.01, 0) * Player.LocalRotation Player.Head.LocalRotation = Rotation(-dy * 0.01, 0, 0) * Player.Head.LocalRotation local dpad = require("controls").DirectionalPadValues Player.Motion = (Player.Forward * dpad.Y + Player.Right * dpad.X) * 50 end
Triggered when a message is submitted through the chat input. Receives the message (string) in parameter.
In the default implementation, messages aren't sent to other connected users.
But it's easy to do using Event and Client.DidReceiveEvent.
-- the default implementation does this: Client.OnChat = function(payload) local message = payload.message -- accessing the message value from the payload table param print(Player.Username .. ": " .. message) Player:TextBubble(message, 3, true) end
Triggered when a player joins the game.
It doesn't mean the Player has spawned, just that there's a new connection.
Client.OnPlayerJoin = function(player) print(player.Username .. " joined the game!") end
Triggered when a player leaves the game.
Client.OnPlayerLeave = function(player) print("So long " .. player.Username .. "!") end
Triggered when the game starts.
This is the very first Lua function to be called, a good place to initialize variables.
Client.OnStart = function() -- add local Player to the World and place it above the center of the map World:AddChild(Player) Player.Position = {Map.Width / 2, Map.Height + 10, Map.Depth / 2} end
Executed ~30 times per second. Provides the elapsed time in seconds as parameter.
-- executed ~30 times per second on each user device Client.Tick = function(dt) print("elapsed:", dt, "seconds") end