Make a kill brick
Setup the part
This is the best starting point for all scripters! We will create the most important object of all obbies.
Start by placing a part inside of your environment. You can color it and scale it as you wish.
Make sure to also anchor your part so that it does not move when a player touches it!
Scripting
Place a ScriptInstance inside of the part we just created.
We are using a ServerScript because the server needs to verify whether the player actually dies or not. If you run it on a local script, other players and the server will not be informed of the players death.
We will first define our part, since the script is inside of the part, we can just do script.Parent
script references our script object that we just created. Then we grab the parent of the script using the .Parent property.
local part = script.Parent -- Our part we created
The Touched Event
Now we need a way to detect when a player touches the part, and this is super simple! All Instace objects in Polytoria have a Touched event which will fire when any other instance touches that part.
We can access the events of a class by using . and the event name.
local part = script.Parent
part.Touched -- The touched event
Connect Method
Now we need to connect it to a function using the :Connect method which all events inherit.
local part = script.Parent
part.Touched:Connect(function(hit) -- We connected the event to an anonymous function with the hit parameter
end)
Do you know why we put hit inside of the function()?
Logic
Now we can write our actual logic inside of our new function that we just created.
Check for player
As explained above, the Touched event will fire no matter what instance hits it. So we need to check if the instance is a player or not.
We do this using the :IsA method.
if hit:IsA("Player") then
We can also directly end the logic if the hit is not a player instead of nesting our code.
if not hit:IsA("Player") then return end -- We return the function if the hit instance is not a player
Kill the player
Now we know it is a player, we can finally kill them! We do this by modifying the player's Health property.
if not hit:IsA("Player") then return end
hit.Health = 0 -- We set the players health to 0
Or damage the player
We can also just damage the player using math instead of instantly killing them.
if not hit:IsA("Player") then return end
hit.Health = hit.Health - 10 -- We remove 10 HP from the player
The player is now either dead or damaged. Good job!
Follow the next tutorial to see how to add a debounce to our kill part.