#Please help with a lava block script
1 messages · Page 1 of 1 (latest)
I think it means that if another part touches a part it does a function, so if a humanoid touches that part it dies
local lava = script.Paremt
local lava is a variable
script.Parent is used when we place a script inside of something we want to use
so now local lava = that part
local function kill(otherpart)
a function is like a recipe, the steps inside are the instructions , so we call the function and it executes the code inside
the kill part is the function name so to call the function you would use
kill()
otherpart sometimes called hit is whatever comes into contact with the part, we use this to get the player who touched the part
Line 4
local partParent = OtherPart.Parent
this is PartParent
its your character
your character just means your model when you run the game
so line 4 is just finding out
who touched the part?
so we can then do something with that player later, such as kill them
line 5
local humanoid = partParent:FindFirstChild("Humanoid")
print(humanoid)
if we print it, it just outputs humanoid
ingame you will notice theres an object called "Humanoid" under your characters model
thats why we use findfirstchild it just means look for the first thing inside the character called "Humanoid"
The reason we use humanoid is because thats where your health is stored
if humanoid then
this is just a "sanity check"
all it means is does humanoid exist?
humanoid.Health = 0
as you saw inside Humanoid is a Health value
so to change that health value and kill the player we just reference it
Humanoid.Health
then set that to 0
i hope ive explained it well any questions feel free
@tropic sky
One thing what does partparent mean and why not part.parent
@hybrid tangle
** You are now Level 1! **
so parentPart is just what they named it
you can name it whatever you want but they named it as its the parent of whatever touched the part
in programming you have parents and children
children are whatever is inside of a parent
if you have a folder and a script inside it
the folder is the parent
the script is the child
wait so is otherpart also just what they named it?
yes usually you name that "hit" as its whatever hit the part but you can name it anything
you have to remember even other parts can set off the OnTouch
not just players so you need to account for that
So is that why for deathblocks we make it check if its a humanoid/player?
yes its a sanity check as it makes sure there is a humanoid before trying to do humanoid.Health = 0
or else it would error saying "um the part touching me doesnt have a humanoid"
local LavaPart = script.Parent
local function kill(hit)
local parent = hit.Parent
if not parent:IsA("Model") then return end
local humanoid = parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
LavaPart.Touched:Connect(kill)
if not parent:IsA("Model") then return end
this is known as a guard, it checks if the thing touching has a model before resuming the code
if its not a model it stops right there
My last question is why is lava.Touched:Connect(kill) not with the other lines
so in programming anything that calls a function has to be under the function
this is because code works top down
Thank you for helping me
local LavaPart = script.Parent
LavaPart.Touched:Connect(function(hit)
local parent = hit.Parent
if not parent:IsA("Model") then return end
local humanoid = parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end)
you can also do it like this if you prefere
no worries