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)
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 end
s
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)