Skip to content

Snippets ​

Type a few letters of a snippet's name at the start of a line in KHIM Engine's script editor - with nothing but the indent before them - and it is offered first among the suggestions. Enter or Tab writes it in, with the cursor where you fill it in.

They are also a quick list of the most common patterns in a KHIM game. Each one, as it is written:

touched ​

When something touches the part (a Script in the part).

lua
-- 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 ​

A part that kills whoever touches it.

lua
-- 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 ​

Save and load a player's data (a Script on the server). See Saving data.

lua
-- 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 ​

lua
-- When a player joins the game
players.joined:connect(function(player)

end)
lua
-- When a player leaves the game
players.left:connect(function(player)

end)

leaderstats ​

A score column in the player list.

lua
-- Adds a Coins column to the player list (Tab), starting at 0
players.joined:connect(function(player)
    player.stats.Coins = 0
end)

prompttriggered ​

When a player presses a Prompt's key (a Script inside the Prompt).

lua
-- When a player presses [E] on the Prompt this Script is in
script.parent.triggered:connect(function(player)

end)

clicked ​

When a player clicks the part (the part needs a Clickable inside it).

lua
-- 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 ​

Repeat forever, once a second.

lua
-- Repeats forever, once every second
while true do

    task.wait(1) -- wait 1 second (without it the game freezes)
end

onkey ​

When the player presses a key (a ClientScript). See Input and camera.

lua
-- When the player presses E (runs only in a ClientScript)
input.onKey("E", function()

end)

netreceive ​

The server receives a message from a client. See Server and client.

lua
-- The server receives "messageName", sent by a ClientScript with net.send("messageName", ...)
net.on("messageName", function(player, ...)

end)

tweenmove ​

Move a part smoothly.

lua
-- Moves the part 5 units up, smoothly over 1 second
local part = script.parent
part:tween({position = part.position + vec3(0, 5, 0)}, 1)

Blocks close themselves ​

Not a snippet, but as handy: press Enter after a line ending in then, do, else, repeat or function(...) and the editor indents the next line and adds the end (or end)) under it. Ctrl Z takes the end back.