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

Expected ')' (to close '(' at line 2), got 'end'?

I have a script where if a part touches a humanoid named Enemy, it changes the value inside it to something else.

However, line 6 has a red line over it and I don't know how to fix it as I'm not very experienced in scripting

if script.Parent.Touched:Connect(function(Hit)
        if Hit.Parent:FindFirstChild("Enemy") then
            Hit.Parent.Enemy.creator.Value = script.Parent.Firer.Value
        end
    end
end)

2 comments

0

you have an extra end

0

@0fficialHen There's an extra end because there's 2 if statement which 1 isn't, and a function. The only problem is this person just needs to remove the "if" on line 1 and remove the 2 end and that'll solve it.

2 answers

✓ Accepted answer

Strange way of hooking up the touch event. neverless, the way you've formatted it is slightly squeed, you need to close the if statement with a then

Like:

if script.Parent.Touched:Connect(function(Hit) -- if statement and event function start
        if Hit.Parent:FindFirstChild("Enemy") then
            Hit.Parent.Enemy.creator.Value = script.Parent.Firer.Value
        end
    end)  then needed since the if statement doesnt have a then word
-- code that will only run once the script loads since this event hookup happens once the script loads!

end -- end of if statement

A better and a more coherent(straight forward) way of doing this is the following:

local TouchedEvent = script.Parent.Touched:Connect(function(Hit) 
        if Hit.Parent:FindFirstChild("Enemy") then
            Hit.Parent.Enemy.creator.Value = script.Parent.Firer.Value
        end
    end) -- touched event end

local timesrun = 0

if TouchedEvent then -- just to show this is only run once!, that's unless this is in some form of loop.
	print("Touched event hooked up!")
	timesrun = timesrun+1
	print("timesrun = "..(timesrun))
end

You dont need to hook up the event top a value since just calling script.Parent.Touched:connect(function(Hit) end) is good enough to get the job done.

Final correct way to do this code:

script.Parent.Touched:Connect(function(Hit) 
	if Hit.Parent:FindFirstChild("Enemy") then
		Hit.Parent.Enemy.creator.Value = script.Parent.Firer.Value
	end
end) 

hope this helps! :)

You have an extra end. You have two if statements, but 3 ends This is how your code should look like.

if script.Parent.Touched:Connect(function(Hit)
        if Hit.Parent:FindFirstChild("Enemy") then
            Hit.Parent.Enemy.creator.Value = script.Parent.Firer.Value
        end
end)

1 comment

0

if script.Parent.Touched:Connect(function(Hit); doing that causes an error. You ain't suppose to put a function inside an if statement.

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