Error with return..?

Hey! So, I’m trying to return the character from a module script, however the second I add the return, I get 2 errors: Both are “Expected end to close then at line 7”, and “Expected EOF, got end” at the last line. This is in a module script, here is my code:

local AtkModule = {}

function AtkModule:Attack(c, Range, Damage, Knockback, Force)
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") then
			if (c.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude < 5 then
				if ((c.HumanoidRootPart.Position - v.HumanoidRootPart.Position)):Dot( c.HumanoidRootPart.CFrame.lookVector ) < 0 then
					return v 
					v.Humanoid:TakeDamage(Damage)
					if Knockback == true then
						print(v.Name)
						local Vel = Instance.new("BodyVelocity")
						Vel.Parent = v.HumanoidRootPart
						Vel.MaxForce = Vector3.new(5000,5000,5000)
						Vel.Velocity = c.HumanoidRootPart.CFrame.LookVector + Vector3.new(250,50,0)
						game.Debris:AddItem(Vel, 0.3)
					end
				end
			end
		end
	end
end
return AtkModule

It worked fine before adding the return, what should I do now?

you have your return v in a weird spot. Move it to the end of a scope body.

perhaps this.

local AtkModule = {}

function AtkModule:Attack(c, Range, Damage, Knockback, Force)
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") then
			if (c.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude < 5 then
				if ((c.HumanoidRootPart.Position - v.HumanoidRootPart.Position)):Dot( c.HumanoidRootPart.CFrame.lookVector ) < 0 then
					
					v.Humanoid:TakeDamage(Damage)
					if Knockback == true then
						print(v.Name)
						local Vel = Instance.new("BodyVelocity")
						Vel.Parent = v.HumanoidRootPart
						Vel.MaxForce = Vector3.new(5000,5000,5000)
						Vel.Velocity = c.HumanoidRootPart.CFrame.LookVector + Vector3.new(250,50,0)
						game.Debris:AddItem(Vel, 0.3)
					end
					return v 
				end
			end
		end
	end
end
return AtkModule
1 Like

It works!! Thanks a lot Andrew!