Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't my lava script kill you on touching the brick?

When I join the game the output says "14:02:56.785 - Workspace.Part.Script:7: unexpected symbol near ')'".

I have no idea what I did wrong so can someone please correct the script for me and explain to me what I did wrong? Thanks!

local lava = script.Parent
local function burn()
	local humanoid = Part.Parent.FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end)
	
lava.Touched:connect(burn)

NEW SCRIPT:

local lava = script.Parent
local function burn()
lava.Touched:connect(function(burn)
	local humanoid = lava.Parent.FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end
end)

1 answer

✓ Accepted answer

You don't need a bracket to close the local function, as you aren't using ":connect". In addition, you didn't define the variable "Part", which should be defined as the function's first argument (ROBLOX always gives this argument when handling Touched events!)

I've corrected it for you here.


local lava = script.Parent
lava.Touched:connect(function(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local char = part.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		if plr then
			plr.Character:BreakJoints()
		end
	end
end)

If this helped, please press "Accept Answer", as it helps others like you find what they're looking for! :)

-- This is what I changed: Instead of defining a function first, then connecting to it, I just connected to a newly defined function. Example:

-- How you did it:
function PrintHelloWorld()
	print("Hello world!")
end

script.Parent.Touched:connect(HelloWorld)

-- How I did it:
script.Parent.Touched:connect(function()
	print("Hello world!")
end)

-- They both do the same! :)

Next, instead of changing the Humanoid's health to zero, I found the character and broke joints.

local char = part.Parent       -- define the player that touched the lava
local plr = game.Players:GetPlayerFromCharacter(char)   -- this function (getplayerfromcharacter(character) finds the player in service "Players".
if plr then                               -- if the player exists, run the code
	plr.Character:BreakJoints()      -- get the player's character, and break its joints (the same as changing its health to zero)
end

Have a nice day! :D

NEW SCRIPT:

-- You don't need a "local function burn() if you're going to connect like I did.
-- You also used ".FindFirstChild", instead of ":FindFirstChild".

lava.Touched:connect(function(victim)
	local humanoid = victim.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end)

8 comments

0

That's nothing like my original script. I want to try to learn Lua but I can't if you corrected my script with a completely different script that I do not even understand.

0

Sorry, but it's doing the same thing in a different way. I'll edit it quickly to explain what I changed.

0

There, I've edited it! Feel free to read, and I hope you'll learn a thing or two! :D

0

I tried changing my script to work my own way but it did not work. Can you explain to me what I did wrong but can you fix it in my version of the script instead of your version? I will edit the new script onto my question.

0

Alright, I'll try.

0

Right, I've added it to the end :)

0

Thanks!

0

You're welcome :)

This discussion is preserved as it was. New answers, comments, and votes are closed.