Help with this rudimentary script

I am a complete beginner to scripting. I’ve been learning from tutorials and the such, and using other’s scripts to make my own.
It’s not so much an issue but rather a query as to why hit.Parent works in this script, and what it even refers to, since hit is the function in this case.

script.Parent.Touched:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			hit.Parent.Humanoid.Health = 0
			
		end
	end)

The script itself is pretty self explanatory, as it kills anyone who steps on it.

2 Likes

When the .Touched function fires, it returns the object that touched it. In this case, you’re naming that variable hit when it gets passed into the function as a parameter.

We can access hit.Parent because we are being passed the reference to the object that touched it, it’s as if you did workspace.PlayerName.LeftLeg.

You can access all the properties, along with setting things and doing anything to it. It’s just a variable with reference to the Part object that touched the Part you’ve got your .Touched event hooked to.

1 Like

So basically the variable “hit” is the object that touched the thing with this script in it, then you need to find hit.Parent.Humanoid because if we just tried to make hit.Humanoid it would return nil.

Better explanation:

leftFoot(hit).Character(hit’s Parent).Humanoid(The Humanoid in the character)
leftFoot(hit).Humanoid oh wait that wont work send an error message REEEEEE
game.Worksapce.Part.Script line 3: Humanoid is not a valid member of leftFoot

1 Like

Thanks. That actually makes sense. I think I’m still confused about the status of the variable in terms of functions, though. I never actually really defined ‘hit’, how does the script know that it refers to the player’s body parts? Sorry for my complete ineptitude.

As @MrLonely1221 and @Johnnyfireball123 said, the hit object is the object that hit it, so it should look something like this:


script.Parent.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild(“Humanoid”) then
			hit.Parent.Humanoid.Health = 0
		end
	end)

The hit is the object that touched the part (script.Parent) and caused the event to fire. The :FindFirstChild() makes sure the part is a child of the character.

2 Likes

I didn’t even really read his code, I just read and thought he was asking for an explanation. Now looking back at it I see the code.

@PerilousPanther ^^ That’s the code that will work properly and do what you’re trying to do a little easier.

The code works perfectly as is, although I understand there are better ways of doing things and worse ways of doing things.

3 Likes

Don’t worry about how the engine detects collisions, this is meant to be abstracted away on purpose. hit.Parent if hit was a limb, like an arm or leg, would be the character. The Players::GetPlayerFromCharacter method returns a player with the provided character.

3 Likes

When you’re making a function you define variables you want passed in

function function1(variable1) --Define our function and the parameter we want to pass into it.
  print("Name:",variable1.Name,"ClassName:",Variable1.ClassName,"Path:",variable1:GetFullName()); --Print the name, type, and path to it
end;

function1(workspace.Terrain); --Call our function with an object we pass in. In this case Terrain.
--[[Expected output
Name: Terrain ClassName: Terrain Path: Workspace.Terrain
--]]

When you connect to an event and pass a function into it, it will pass whatever that event returns, to that function as a parameter (our variable1 above.)

function onPlayerAdded(playerObject)
  print("Name:",playerObject.Name,"Path:",playerObject:GetFullName());
end;

game:GetService("Players").PlayerAdded:Connect(onPlayerAdded); --PlayerAdded is an event (RBXScriptSignal) that returns an Instance of type Player to the callback function defined in the :Connect(function).
--This can also be stored in a variable as :Connect(function) returns a Connection (RBXScriptConnection) that can be disconnected when you no longer want to listen to the event.

--[[Expected output - Each time a player joins
Name: MrLonely1221 Path: Players.MrLonely1221
Name: PerilousPanther Path: Players.PerilousPanther
--]]

Parameters in functions work the same way regular variables do.

local me = game:GetService("Players").MrLonely1221;

function onPlayerAdded(playerObject)
  print("Name:",playerObject.Name,"Path:",playerObject:GetFullName());
end;

onPlayerAdded(me)l
game:GetService("Players").PlayerAdded:Connect(onPlayerAdded);

--[[Expected output - Say I was in game when the code ran, then you joined.
Name: MrLonely1221 Path: Players.MrLonely1221
Name: PerilousPanther Path: Players.PerilousPanther
--]]

I hope this helps understand it a bit more? And that this wasn’t too confusing.
Here are some of the references I mentioned:
PlayerAdded Wiki
RBXScriptSignal Wiki

1 Like

Thanks a lot for the help. This clears up a lot. However, I’m sure I’m just missing something obvious. In all the examples you provide you create a variable which is later defined. Like in the first function where you create the function and then insert the terrain as the variable.
For my example is this something I did without realising, or did the code automatically assume I was talking about the player parts given the context of the code?

In your code, you’re connecting to an event. This event when connected to, calls the function you defined in there with the parameters it passes in. In the case of .Touched() when connecting to it, it passes the Instance that touched it.

For your code, you’re trying to get the player from it, then checking if there is a player. After that you’re setting the health to 0. The parameter could be anything it isn’t required to be hit.

1 Like

Yeah, I get it now. Thanks for all the help. I’m sorry it took so long. :smile: