Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Roblox Help With Scripting?

Asked by 9 years ago

Ok here is my script i keep getting a error part 1 is not a valid member of workspace local part = Instance.new("Part") part.Name = "Part1" part.Parent = workspace function game.workspace.Part1(onTouch) game.Workspace.Player1.Head:remove()

end

0
You've posted a broken script that does not contain the issue you're talking about. 1waffle1 2908 — 9y

2 answers

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

The code you've posted will not run, so I'll try to interpret what it is supposed to mean:

local part=Instance.new("Part")
part.Name="Part1"
part.Parent=workspace
part.Touched:connect(function()
    workspace:WaitForChild("Player1"):WaitForChild("Head"):Destroy()
end)

It is possible that you meant to say Player1 is not a valid member of workspace and not Part1, in which case this will wait for the player (and its head) to exist before continuing.

Ad
Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

Problem

  1. 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 ~~~~~~~.
  2. Lua IS CASE SENSITIVE, that can not be stressed enough. You can either use game.Workspace or workspace, you can not use both.
  3. If you use a variable to make a part, then you can use that variable to connect to a function.
  4. 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().

local part = Instance.new("Part")
part.Name = "Part1"
part.Parent = workspace
part.Touched:connect(function() 
    game.Workspace.Player1.Head:remove()
end) --We need a end parenthesis to indicate that's the end of the function.

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.

local part = Instance.new("Part")
part.Name = "Part1"
part.Parent = workspace
part.Touched:connect(function(hittingObject) --hittingObject will be the object that hits the part.
    if hittingObject.Parent:FindFirstChild('Head') then
        hittingObject.Parent.Head:Destroy()
    end
end)

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!

Answer this question