Problem
- First off, place your code in a code block, that way we can read it easier. Your code should go right in between the tildes
~~~~~~~
.
- Lua IS CASE SENSITIVE, that can not be stressed enough. You can either use
game.Workspace
or workspace
, you can not use both.
- If you use a variable to make a part, then you can use that variable to connect to a function.
- You need to use a event to connect to a function. What it looks like you were trying to do a weird callback function. You would want to use the
.Touched
event.
Solution
You will want to use the variable 'part' you have established above, then add the .Touched event, followed by a connection into a function using :connect()
.
1 | local part = Instance.new( "Part" ) |
4 | part.Touched:connect( function () |
5 | game.Workspace.Player 1. Head:remove() |
The :remove()
function is deprecated, it is always recommended you use a different method if one is deprecated as the code is only kept for compatibility. The recommended function to use is :Destroy()
. Also, your code will only work if there is a Player1 in the game. If you want to remove the head of anything that hits the part, you can use a variable for the Touched event. Some events will return values such as the touched event, which will return the object that hit the part.
1 | local part = Instance.new( "Part" ) |
4 | part.Touched:connect( function (hittingObject) |
5 | if hittingObject.Parent:FindFirstChild( 'Head' ) then |
6 | hittingObject.Parent.Head:Destroy() |
For line 5, your character is a model, when something hits the part, it will check that hitting part's parent to see if it has a head. Most likely for this script, it will be your arm or leg hitting the part, those are both part of the model. We take that leg or arm's parent and find the first object that is named "Head". If there is a object that is named "Head", then on line 6 the script will go to the hitting object's parent, and destroy the part in it named "Head".
Now you have basically made a kill script.
If this helped, do not forget to upvote and if it solved your problem then Accept the Answer. If you need more information or explanation, feel free to comment!