Snippet
Gõ vài chữ đầu tên một snippet ở đầu dòng trong trình soạn script của KHIM Engine - trước nó chỉ có thụt lề - và nó được gợi ý đầu tiên. Enter hoặc Tab sẽ viết nó ra, con trỏ đặt ở chỗ bạn cần điền.
Đây cũng là danh sách nhanh các mẫu code hay gặp nhất trong game KHIM. Từng snippet, đúng như được viết ra (chú thích trong code bằng tiếng Anh, như trong trình soạn):
touched
Khi có gì chạm vào part (một Script trong part).
-- When something touches the part this Script is in
script.parent.touched:connect(function(hit)
local player = players.fromPart(hit) -- the player who touched it (nil if it was not a player)
if player then
end
end)killbrick
Part giết bất kỳ ai chạm vào.
-- Kills any player who touches this part
script.parent.touched:connect(function(hit)
local player = players.fromPart(hit)
if player then
player.health = 0
end
end)savedata
Lưu và tải dữ liệu người chơi (một Script trên server). Xem Lưu dữ liệu.
-- Saves and loads each player's Coins (runs only in a Script on the server)
local store = data.store("PlayerData")
players.joined:connect(function(player)
player.stats.Coins = 0 -- the value before anything was saved
if player.userId > 0 then -- guests are not saved
local ok, saved = pcall(function() return store:get("user_" .. player.userId) end)
if ok and saved then player.stats.Coins = saved end
end
end)
players.left:connect(function(player)
if player.userId > 0 then
pcall(function() store:set("user_" .. player.userId, player.stats.Coins) end)
end
end)playerjoined / playerleft
-- When a player joins the game
players.joined:connect(function(player)
end)-- When a player leaves the game
players.left:connect(function(player)
end)leaderstats
Một cột điểm trong bảng người chơi.
-- Adds a Coins column to the player list (Tab), starting at 0
players.joined:connect(function(player)
player.stats.Coins = 0
end)prompttriggered
Khi người chơi bấm phím của một Prompt (một Script bên trong Prompt).
-- When a player presses [E] on the Prompt this Script is in
script.parent.triggered:connect(function(player)
end)clicked
Khi người chơi bấm vào part (part cần có một Clickable bên trong).
-- When a player clicks the part (the part needs a Clickable inside it)
local clickable = script.parent:waitFor("Clickable")
clickable.clicked:connect(function(player)
end)whileloop
Lặp mãi mãi, mỗi giây một lần.
-- Repeats forever, once every second
while true do
task.wait(1) -- wait 1 second (without it the game freezes)
endonkey
Khi người chơi bấm một phím (ClientScript). Xem Phím, chuột và camera.
-- When the player presses E (runs only in a ClientScript)
input.onKey("E", function()
end)netreceive
Server nhận tin nhắn từ client. Xem Server và client.
-- The server receives "messageName", sent by a ClientScript with net.send("messageName", ...)
net.on("messageName", function(player, ...)
end)tweenmove
Di chuyển một part mượt mà.
-- Moves the part 5 units up, smoothly over 1 second
local part = script.parent
part:tween({position = part.position + vec3(0, 5, 0)}, 1)Khối lệnh tự đóng
Không phải snippet, nhưng cũng tiện: bấm Enter sau một dòng kết thúc bằng then, do, else, repeat hay function(...) là trình soạn thụt lề dòng mới và thêm end (hoặc end)) bên dưới. Ctrl Z lấy lại end đó.