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

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

Asked by
das1657 20
3 years ago

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)
0
you have an extra end 0fficialHen 5 — 3y
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. MarkedTomato 810 — 3y

2 answers

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

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! :)

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
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. MarkedTomato 810 — 3y

Answer this question