Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
|
| 30 Apr 2013 04:27 PM |
Just started scripting. This is the first function i've made. What's wrong with it? It doesn't seem to work.
function Kill(part) Workspace.Player1.Head:Remove() end
brick.Touched:connect(onTouch)
It's in a brick called 'Lava' |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 30 Apr 2013 04:35 PM |
script.Parent.Touched:connect(Kill)
The thing in parenthesis in the connection line has to be the name of your function (there are exceptions, but don't worry about them).
Also, the beginning needs to be an existing object. Just use 'script.Parent' for things when you put the script in an actual object. |
|
|
| Report Abuse |
|
|
Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
|
| 30 Apr 2013 04:41 PM |
| Thanks! Now i understand! The script works! |
|
|
| Report Abuse |
|
|
Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
|
| 30 Apr 2013 04:42 PM |
| I got another problem.. It kills me the first time. But after it doesn't affect me. What's causing this? |
|
|
| Report Abuse |
|
|
Iciness
|
  |
| Joined: 18 Jan 2012 |
| Total Posts: 20969 |
|
|
| 30 Apr 2013 04:44 PM |
| The script simply ends it doesn't loop. |
|
|
| Report Abuse |
|
|
Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
|
| 30 Apr 2013 04:48 PM |
| How does the loop event work than? :) |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 30 Apr 2013 04:50 PM |
"The script simply ends it doesn't loop."
No where near correct.
The reason is that the script is erroring. Check the output (or rather, keep it open in studio when testing). Your head is only there once. When you touch it (with all your limbs after dying) you are activating it multiple times. Again, you only have one head, and it is removed the first time.
Add an if statement
if Workspace.Player1:FindFirstChild("Head") then Workspace.Player1.Head:Destroy() end |
|
|
| Report Abuse |
|
|
Iciness
|
  |
| Joined: 18 Jan 2012 |
| Total Posts: 20969 |
|
|
| 30 Apr 2013 04:50 PM |
| I am no pro scripter but I believe your "end" should be the last thing in the script. |
|
|
| Report Abuse |
|
|
CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
|
| 30 Apr 2013 04:54 PM |
| Shouldn't it also be game.Workspace, not Workspace. IDK I may be wrong. |
|
|
| Report Abuse |
|
|
Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
|
| 30 Apr 2013 04:58 PM |
function Kill(part) if Workspace.Player1:FindFirstChild("Head") then Workspace.Player1.Head:Destroy() end
script.Parent.Touched:connect(Kill)
Still doesn't work. Sorry if i'm interrupting you. But i'm quite confused.. |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 30 Apr 2013 05:00 PM |
function Kill(part) if Workspace:FindFirstChild("Player1") and Workspace.Player1:FindFirstChild("Head") then Workspace.Player1.Head:Destroy() end end
script.Parent.Touched:connect(Kill) |
|
|
| Report Abuse |
|
|
CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
|
| 30 Apr 2013 05:01 PM |
function Kill(part) if game.Workspace.Player1:findFirstChild("Head") ~= nil then game.Workspace.Player1.Head:Destroy() end end -- need 2 ends because you started a seconds functions
script.Parent.Touched:connect(Kill) |
|
|
| Report Abuse |
|
|
CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
|
| 30 Apr 2013 05:04 PM |
So this script is in a brick?
Easiest way to kill the player that touched brick is:
function onTouch(hit) if hit.Parent:findFirstChild("Humanoid") ~= nil then hit.Parent:BreakJoints() end end
script.Parent.Touched:connect(onTouch)
or something along those lines. |
|
|
| Report Abuse |
|
|
|
| 30 Apr 2013 05:06 PM |
@carlos that breaks, rather this: script.Parent.Touched:connect(function(hit) if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then hit.Parent:BreakJoints() end end) |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 30 Apr 2013 05:08 PM |
Mmm...even better, though I wouldn't use this because you'll want to learn from previous function-connection separate parties.
script["Parent"]["Touched"]:connect(function(a) coroutine.resume(coroutine.create(function( ) a.Parent.Head:Destroy() end) ) end) |
|
|
| Report Abuse |
|
|
Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
|
| 30 Apr 2013 05:08 PM |
I'm really getting confused now.
|
|
|
| Report Abuse |
|
|
|
| 30 Apr 2013 05:11 PM |
Haha, yes, Nic. He should use this, because he shouldn't learn the complex things first:
function onTouched(hit) if hit then if hit.Parent then if hit.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid.Health = 0 end end end end script.Parent.Touched:connect(onTouched) |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 30 Apr 2013 05:12 PM |
....You guys should start using advanced stuff, only then when you can do almost anything with simple stuff....
The part simply get's touched several times, and code tries to remove head, even if it's gone already. |
|
|
| Report Abuse |
|
|
|
| 30 Apr 2013 05:15 PM |
@zars I doubt you read any of the posts apart from the first couple.. |
|
|
| Report Abuse |
|
|
Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
| |
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 30 Apr 2013 05:21 PM |
Since everyone is posting something different, obviously OP get's confused as crazy.
brick = script.Parent--I guess this is what you meant
function Kill(part) --Workspace.Player1.Head:Remove() Doing this will remove only your head. Works fine for testing, but really pointless + script would break if 'Player1' wouldn't be in game. if part.Parent:FindFirstChild("Head") then--This is the most important part you were missing. The script tried to destroy something that didn't exist, now it checks before destroying. part.Parent.Head:Destroy()--You should use destroy instead of remove. Just new stuff, that you should use. end end
brick.Touched:connect(Kill)--This part connects to function. Your function is named 'Kill'
--Also you didn't specify what is this 'brick' |
|
|
| Report Abuse |
|
|
|
| 30 Apr 2013 05:23 PM |
Haha, your script would break. A lot.
>part == nil (part is destroyed before function is called) >part.Parent == nil (part is removed from datastructure game before function is called) |
|
|
| Report Abuse |
|
|
Hergest
|
  |
| Joined: 17 May 2011 |
| Total Posts: 309 |
|
|
| 30 Apr 2013 05:27 PM |
| Thanks a lot guys. You all have been very helpfull. Now i managed to create my first working function. Thanks again! :) |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 30 Apr 2013 05:29 PM |
Yes, in past it was quite useful to have
if part then
but now it's not that necessary, since how often do you have parts that instantly get's deleted on hit? Back when guns that had physical projectiles, it was pretty vital. |
|
|
| Report Abuse |
|
|
CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
|
| 30 Apr 2013 06:01 PM |
| I'm glad your script is working now |
|
|
| Report Abuse |
|
|