Warp/docs/guide/example.md

78 lines
1.9 KiB
Markdown
Raw Normal View History

2026-02-13 07:58:54 +00:00
# Example <Badge type="tip" text="1.1" />
2024-01-05 12:14:38 +00:00
Let's try and play something with Warp!
::: code-group
2026-02-13 07:58:54 +00:00
```luau [Schemas]
local Schema = require(path.to.warp).Buffer.Schema
2024-01-05 12:14:38 +00:00
2026-02-13 07:58:54 +00:00
return {
Example = Schema.array(Schema.string),
Ping = Schema.string,
Pong = Schema.string,
PingAll = Schema.string,
2026-02-13 07:58:54 +00:00
}
```
```luau [Server]
local Warp = require(path.to.warp).Server()
local Schemas = require(path.to.schemas)
2024-01-05 12:14:38 +00:00
2026-02-13 07:58:54 +00:00
-- Use schemas
for eventName, schema in Schemas do
Warp.useSchema(eventName, schema)
2026-02-13 07:58:54 +00:00
end
2024-01-05 12:14:38 +00:00
2026-02-13 07:58:54 +00:00
Warp.Connect("Example", function(player, arg)
print(table.unpack(arg))
return "Hey!"
2026-02-13 07:58:54 +00:00
end)
Warp.Connect("Ping", function(player, ping)
if ping then
print("PING!")
Warp.Fire("Pong", true, player, "pong!") -- Fire to spesific player through reliable event
Warp.Fire("PingAll", true, "ey!") -- Fire to all clients through reliable event
end
2024-01-05 12:14:38 +00:00
end)
```
2026-02-13 07:58:54 +00:00
```luau [Client]
2024-01-05 12:14:38 +00:00
local Players = game:GetService("Players")
2026-02-13 17:02:17 +00:00
local Warp = require(game.ReplicatedStorage.WarpNew).Client()
local Schemas = require(game.ReplicatedStorage.Schemas)
2024-01-05 12:14:38 +00:00
2026-02-13 07:58:54 +00:00
-- Use schemas
for eventName, schema in Schemas do
Warp.useSchema(eventName, schema)
2026-02-13 07:58:54 +00:00
end
2024-01-05 12:14:38 +00:00
-- Connect the events
local connection1
2026-02-13 07:58:54 +00:00
connection1 = Warp.Connect("Pong", function(pong: boolean) -- we store the connection so we can disconnect it later
if pong then
print("PONG!")
end
2024-01-05 12:14:38 +00:00
end)
2026-02-13 07:58:54 +00:00
Warp.Connect("PingAll", function(isPing: boolean)
if isPing then
print("I GET PINGED!")
end
2024-01-05 12:14:38 +00:00
end)
task.wait(3) -- lets wait a few seconds, let the server do the things first!
2024-01-05 12:14:38 +00:00
-- Try request a event from server!
2026-02-13 07:58:54 +00:00
print(Warp.Invoke("Example", 1, { "Hello!", `this is from: @{Players.LocalPlayer.Name}` }))
2024-01-05 12:14:38 +00:00
-- Do a ping & pong to server!
2026-02-13 07:58:54 +00:00
Warp.Fire("Ping", true, "ping!") -- we send through reliable event
2024-01-05 12:14:38 +00:00
2026-02-13 17:02:17 +00:00
task.wait(1) -- lets wait for a second!
2024-01-05 12:14:38 +00:00
-- Disconnect All the events
connection1:Disconnect()
2026-02-13 07:58:54 +00:00
-- or just disconnect spesific connection
Warp.DisconnectAll("PingAll")
2024-01-05 12:14:38 +00:00
-- Destroying/Deleting a Event?
2026-02-13 07:58:54 +00:00
Warp.Destroy("Pong")
2024-05-29 09:58:20 +00:00
```
:::