ModuleScript Function returning before it end

so im making a turn based game and apparently the script just wait forever becouse the module return nil, but in the image below you can see it return true, the point in the end is that it try to get the value before the script finish

Server Script:

if AttackFirst == Character then
			ChatService:Chat(Character,PlayerMove.Name,2)
			
			local fin = false
			local fin2 = false
			
			fin = AttackModule[PlayerMove.Name](Character,Npc)
			
			print(fin)
			
			repeat wait() until fin == true
			
			print("Fire2")
			
			fin2 = AttackModule[PlayerMove.Name](Npc,Character)
			
			repeat wait() until fin2 == true
			
			AttackEvent:FireClient(player)
		end

Module:

local ServerStore = game:GetService("ServerStorage")

local Animations = ServerStore:WaitForChild("Animations")

local Attacks = {
	["Punch"] = function(Character,Enemy)
		local Humanoid = Character.Humanoid
		local EnemyHumanoid = Enemy.Humanoid 
		
		local StartCharCFrame = Character.Torso.CFrame
		local StartCharPosition = Character.Torso.Position
		local StartEnemyCFrame = Enemy.Torso.CFrame
		local StartEnemyPosition = Enemy.Torso.Position
		
		local Animation = Animations.Fists.Punch
		local Track = Humanoid:LoadAnimation(Animation)
		
		if Character.HumanoidRootPart.Anchored == true then Character.HumanoidRootPart.Anchored = false end
		if Enemy.HumanoidRootPart.Anchored == true then Enemy.HumanoidRootPart.Anchored = false end		
		
		Humanoid:MoveTo(Enemy.Torso.Position + Vector3.new(0,0,-2))
		
		local targetReached = false
		local connection
		connection = Humanoid.MoveToFinished:Connect(function(reached)
			targetReached = true
			connection:Disconnect()
			connection = nil
			Track:Play()
			Track.Stopped:wait()
			wait(0.1)
			EnemyHumanoid:TakeDamage(4)
			
			Character.Torso.CFrame = StartCharCFrame
			Enemy.Torso.CFrame = StartEnemyCFrame

			wait(0.5)

			if Character.HumanoidRootPart.Anchored == false then Character.HumanoidRootPart.Anchored = true end
			if Enemy.HumanoidRootPart.Anchored == false then Enemy.HumanoidRootPart.Anchored = true end		
			
			print("Returned")
			
			return true
		end)
	end,
}

return Attacks

Result:
https://gyazo.com/eac834839ac8ff1f5d11fdce9d06c95a

Maybe

Hangs forever?

What is in ServerStorage?

no no the code still print the “Returned” stuff but the server script get nil from the return

If you got “Returned” that means that “Punch” was called, meaning your module didn’t return nil

ik in fact the function returned nil, i think if you read the code its pretty understanble and obvius im talking about the function

oh sorry

here you are returning from the callback, don’t do that.
Try instead

local returnValue
connection = Humanoid.MoveToFinished:Connect(function(reached)
	... - Other stuff you have here
	returnValue = true
end)
return returnValue

Just tried, and nothing its the same


it return nil
and the code only do the first punch then stop again
well instead of trying to fix this you got any other way to make the server script wait for the module function to finish?

Look, it’s late at night and I made yet another grueling mistake.

Maybe you could repeat wait() until returnValue ~= nil before returning? I’m too lazy to do it myself

thanks tho ill keep trying myself if anyone here got an idea pls help me

I still need help, for anyone reading, the issue in not fixed nad i still have no idea if you pass on the post share me some ideas on how to fix

Are you wanting to wait to return until MoveToFinished?

local ServerStore = game:GetService("ServerStorage")

local Animations = ServerStore:WaitForChild("Animations")

local Attacks = {
	["Punch"] = function(Character,Enemy)
		local Humanoid = Character.Humanoid
		local EnemyHumanoid = Enemy.Humanoid 
		
		local StartCharCFrame = Character.Torso.CFrame
		local StartCharPosition = Character.Torso.Position
		local StartEnemyCFrame = Enemy.Torso.CFrame
		local StartEnemyPosition = Enemy.Torso.Position
		
		local Animation = Animations.Fists.Punch
		local Track = Humanoid:LoadAnimation(Animation)
		
		if Character.HumanoidRootPart.Anchored == true then Character.HumanoidRootPart.Anchored = false end
		if Enemy.HumanoidRootPart.Anchored == true then Enemy.HumanoidRootPart.Anchored = false end		
		
		Humanoid:MoveTo(Enemy.Torso.Position + Vector3.new(0,0,-2))
		
		local targetReached = false
		Humanoid.MoveToFinished:Wait()
		targetReached = true
		connection:Disconnect()
		connection = nil
		Track:Play()
		Track.Stopped:wait()
		wait(0.1)
		EnemyHumanoid:TakeDamage(4)
		
		Character.Torso.CFrame = StartCharCFrame
		Enemy.Torso.CFrame = StartEnemyCFrame

		wait(0.5)

		if Character.HumanoidRootPart.Anchored == false then Character.HumanoidRootPart.Anchored = true end
		if Enemy.HumanoidRootPart.Anchored == false then Enemy.HumanoidRootPart.Anchored = true end		
		
		print("Returned")
		
		return true
	end,
}

Well yeah, i want the move called “Punch” to finish then make the script run again, thats why i tried using repeat wait() until