In Introduction to scripting, you learned how to make changes in a game in a loop over time. What if you want to make changes based on user behavior? In this tutorial, you'll learn how to make a deadly lava floor which kills users when they step on it.
설정
You need a place in your world to put the deadly lava. If you followed the Introduction to scripting course, the lava floor would fit nicely in the gap covered by the disappearing platforms.
Part를 삽입하고 세계에서 적절한 위치로 이동합니다. 이름을 LavaFloor로 지정합니다.
그것의 크기를 조정하여 둘러싸인 공간의 바닥을 덮도록 합니다.

Material 속성을 Neon으로 설정하고 Color를 주황색 음영으로 설정하여 바닥이 용암처럼 보이도록 합니다.


LavaFloor 파트에 Script를 삽입하고 이름을 Kill로 변경합니다.

기본 코드를 제거하고 용암에 대한 변수를 생성합니다.
local lava = script.Parent
이벤트에 연결
Use an event to detect when a user touches the lava. Every part has a Touched event that fires when something touches it. You can connect to this event to run a function when it fires.
kill이라는 새 함수를 선언합니다.
점을 사용하여 용암 객체의 Touched 이벤트에 접근합니다: lava.Touched.
Touched 이벤트에서 Connect 함수를 호출하고, 파트가 터치될 때 호출할 함수로 kill()을 전달합니다.
local lava = script.Parentlocal function kill()endlava.Touched:Connect(kill)
Any code you write in the kill function will now run whenever something touches the lava. Note that a colon is used for the Connect function, not a dot - don't worry about why at this point, just remember the difference.
접촉하는 파트 가져오기
To kill the user, the function will need an object associated with that user. A part's Touched event can provide the "other part" that touched it — but only if you request it by making it a parameter of the function.
Parameters are definitions of what a function expects to receive when it's called. A parameter can be used in a function just like any other variable. You can pass information to a parameter by including it in the parentheses when a function is called. Parameters are defined in the parentheses on the first line of a function. Create a parameter called otherPart for the kill function.
local lava = script.Parent
local function kill(otherPart)
end
lava.Touched:Connect(kill)When the kill function is called, the otherPart parameter will represent the part that touched the lava floor, and the code you'll write in the function will be able to use it.
캐릭터와 휴머노이드
When a user touches the lava, Roblox can detect the specific body part of the user that touched it, such as the left leg or right foot. This part is in the user's Character model, which contains all of the objects that make up the user's avatar in the game, including:
- The individual body parts of the user such as the head, limbs, and torso.
- Any clothing and accessories worn by the user.
- The Humanoid, a special object which contains many properties related to the user, including the user's health.
- The HumanoidRootPart which controls the user's movement.
As previously noted, any body part that touches the lava is part of the Character model, so you can get a reference to that character with otherPart.Parent. Create a variable to store the parent of the part that touched the lava floor.
local lava = script.Parent
local function kill(otherPart)
local partParent = otherPart.Parent
end
lava.Touched:Connect(kill)From the character model, you'll need to get the Humanoid object in order to kill the user. You can do this with the FindFirstChild() function—just pass it the name of the thing you're looking for and it provides the first matching child it finds in that object. Call FindFirstChild() on the partParent variable with "Humanoid" as the child to find, and store the result in a new variable called humanoid.
local lava = script.Parent
local function kill(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
end
lava.Touched:Connect(kill)휴머노이드 확인
You can easily check if the Humanoid was found using an if statement. The code in an if statement will only run if the condition defined in the first line is true.
There are a variety of operators that can be used to build more complex conditions which you'll encounter in future courses - for now, just put the humanoid variable there. Create an if statement with humanoid as the condition.
local lava = script.Parent
local function kill(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if humanoid then
end
end
lava.Touched:Connect(kill)캐릭터 건강 설정
Now that the Humanoid has been checked, its properties can be changed. If you set its Health property to 0, the associated Character dies. In the body of the if statement, set the Health property of humanoid to 0.
local function kill(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
lava.Touched:Connect(kill)With that, your lava floor is complete! Playtest your game and you should find that your deadly lava successfully kills users on contact. Try using your lava as an extra challenge in an obby, or as a boundary for a world.
최종 코드
local lava = script.Parent
local function kill(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
lava.Touched:Connect(kill)