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

Kill Script Error? - Easy Question!

Asked by 8 years ago
Edited by M39a9am3R 8 years ago

Hi, so I'm a bit new to Programming and just dived into Roblox. The goal I'm trying to accomplish here is making a script for a part in which will insta-kill any roblox player that steps on it.

Here is my code:

01local Part = script.Parent
02 
03function local onTouch(hit)
04human = hit.Parent:FindFirstChild("Humanoid") -- Finds the "Humanoid" part
05if h ~= nil then
06human.Health = 0
07end
08end
09 
10if Part.Touched then -- If something is touching the part
11    onTouch(Part.Touched) -- Invokes the "onTouch" function with the character's leg being it's parameter
12end

I've written down an explanation (or what I think it means at least). However it does not work, I also tried putting an infinite while loop on the if statement but that just crashes the game. If someone can spot the error to my code or suggest me a new form of code I would really appreciate it.

I would also like to note, that I'm currently a noob, so an explanation on each line would help and why you chose this etc etc. Thank you very much

2 answers

Log in to vote
2
Answered by
anerth 25
8 years ago
Edited 8 years ago

Hey! Welcome to scripting! I put the changes in the code. If you've got questions, I'm right here!

01local Part = script.Parent -- Set the part
02 
03function onTouch(hit)
04    if hit ~= nil then -- Check if hit is not nil
05        human = hit.Parent:FindFirstChild("Humanoid") -- Finds the "Humanoid" part
06        if human ~= nil then -- check if human is not nil
07            human.Health = 0 -- kill player
08        end
09    end
10end
11 
12script.Parent.Touched:connect(onTouch) -- You can just use this line rather than the if Part.Touched statement

Also, if you do a while loop, be sure to do this:

1while true do
2    --Code here
3    wait() -- you can leave the brackets blank.
4end
0
You could also do: `while wait() do end` Vingam_Securis 213 — 8y
0
Also, if you were checking for something, 'repeat wait() until' anerth 25 — 8y
0
I have a question, wouldn't the line "script.Parent.Touched:connect(onTouch)" require a loop to constantly check it? blueboy90780 74 — 8y
0
I also found that removing the if statement works as well, but creates more lag. blueboy90780 74 — 8y
0
Touched is one more of an event, not a property. This means anytime anything touches the brick, the event will fire. The event requires a function to process any code, with the Touched event it will provide a brick that touched it. If you're just waiting for an event to happen, you could do game.Workspace.Part.Touched:Wait() and it will wait until part in workspace is touched. M39a9am3R 3210 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Here is my simplified version,

1script.Parent.Touched:connect(function(hit)
2local human = hit.Parent:WaitForChild("Humanoid") -- variable for the humanoid
3human.Health = 0 -- kills the player
4end)

this should work

Answer this question