Touch, prompts and zones
How players interact with the world: touching parts, clicking them, pressing a key near them, walking into an area. Much of it needs no code at all.
Gameplay with no code
Put one of these inside a part (Insert Object > Gameplay (no script needed)), set a number or two in the Inspector, and press Play. An obby's whole kit is a few inserts.
| Object | When a player touches its part | Settings |
|---|---|---|
| Teleporter | They appear standing on another part | destination (a part). Two Teleporters that name each other make a two-way portal |
| Hazard | It takes health at once, and again each second they stay | damage (100, a whole bar: deadly at once; negative heals) |
| Checkpoint | From now on they come back here after dying | - |
| Pickup | It adds to their stat and its part disappears for a while | stat ("Coins"), amount (1), respawnTime (10 s; 0 = never) |
| Boost | More walk speed and jump power for a while | walkSpeed, jumpPower, duration (seconds) |
And four that need no player to touch them:
| Object | What it does | Settings |
|---|---|---|
| Motion | Its part (and the parts inside it) moves and turns by itself: a moving platform, a lift, a spinning blade, a swinging hammer | move (a vec3 of units), turn (degrees), pivot, time (each way), pause (at each end), delay, reverses, looped, smooth, relative |
| Spawner | Makes a copy of a part or a Model every few seconds where its part is: a dropper's ore, falling rocks | template, interval, limit (at once), lifetime, velocity, relative |
| Seat | A player who touches it sits down; jumping stands them up | occupant, sat, left, sit(player), stand() |
| VehicleSeat | A seat that drives: the sitter's keys turn the motor Hinges of its Model. Insert > Vehicle (car) makes one ready to drive | maxSpeed, torque, turnSpeed; read throttle, steer |
The server does all of it, so a player can't collect a coin or skip lava by cheating.
Adding to them with a script
Each fires triggered(player) when it acts, and an event with a more telling name: Teleporter teleported, Hazard hurt, Checkpoint reached, Pickup collected, Boost boosted. A script adds to a brick without replacing it:
world.Coin.Pickup.collected:connect(function(player)
print(player.name .. " has " .. player.stats.Coins .. " coins")
end)
world.Finish.Checkpoint.reached:connect(function(player)
chat.send(player.displayName .. " reached the finish!")
end)
local lava = new("Hazard", world.Floor) -- and they are made like anything else
lava.damage = 25Every one has enabled.
Touching
touched fires when something touches a part - a player's character or another part. touchEnded when it stops.
local pad = world.SpeedPad
pad.touched:connect(function(hit)
local player = players.fromPart(hit)
if player then
player.walkSpeed = 40
end
end)touched fires many times while someone stands on a part. To act once, remember who is on it:
local onPad = {}
pad.touched:connect(function(hit)
local player = players.fromPart(hit)
if not player or onPad[player.name] then return end
onPad[player.name] = true
player:damage(10) -- once, not every frame they stand there
task.wait(1)
onPad[player.name] = nil
end)Clicking: Clickable
Put a Clickable in a part and players can click (or tap) it:
local button = world.Button
local clicker = new("Clickable", button)
clicker.maxDistance = 20 -- units
clicker.clicked:connect(function(player)
print(player.name .. " pressed the button")
end)Pressing a key: Prompt
A Prompt in a part shows a small panel - "[E] Open" - to players near it. Pressing the key, or clicking or tapping the panel, fires triggered on the server:
local door = world.Door
local prompt = new("Prompt", door)
prompt.text = "Open"
prompt.key = "E"
prompt.maxDistance = 8
prompt.triggered:connect(function(player)
door.canCollide = false
door.transparency = 0.7
task.wait(3)
door.canCollide = true
door.transparency = 0
end)Only the nearest Prompt in reach shows, and the server checks the player really is in reach.
Text over a part: Billboard
local sign = new("Billboard", world.Shop)
sign.text = "SHOP"
sign.offset = 2 -- units above the part's top
sign.textSize = 24
sign.maxDistance = 80It always faces the camera, stays the same size on screen, and fades when something stands between it and the camera. For writing on a face of a part, use a Sign (text, face, textColor, fit).
Areas: Zone
A Zone makes its part an area that says who comes in and goes out - a shop, a safe zone, a finish line. KHIM Engine makes the part see-through, anchored and walk-through when you add one.
local zone = world.ShopArea.Zone
zone.entered:connect(function(player)
net.send(player, "openShop")
end)
zone.left:connect(function(player)
net.send(player, "closeShop")
end)
print(#zone:players() .. " in the shop")| Member | What it is |
|---|---|
entered, left | A player came in / went out |
partEntered, partLeft | A loose part came in / went out |
players(), parts() | Who and what is inside now |
contains(position or player or part) | Whether it is inside |
enabled | On or off |
A player is inside when the line through their body, feet to head, passes through the part - so a flat pad on the floor works as well as a tall box.
Timers
A Timer fires after a time, once or again and again - a round's clock, a bomb's fuse, a wave of enemies:
local timer = new("Timer", world)
timer.interval = 30
timer.repeats = true
timer.fired:connect(function()
chat.send("A new wave is coming!")
end)
timer:start()
-- timer.timeLeft, timer.running, timer:stop()